2016-04-26 32 views
0

我有下面顯示的代碼,我試圖根據用戶輸入插入某些值到數據庫中。我需要驗證用戶輸入,這就是爲什麼我使用SQL命令進行插入。如何在SQL commnad中使用cmd參數中的子查詢?

我可以運行此使用SQL適配器,但是,這並不似乎是有效的方法,因爲我需要做大量的驗證用戶輸入的.....

問題:

我是新到這個C#和我不知道如何在cmd參數中使用子查詢。任何人都可以讓我知道如何使下面的代碼工作?

if (MessageBox.Show("Do you really want to Insert these values?", "Confirm Insert", MessageBoxButtons.YesNo) == DialogResult.Yes) 
{ 
    SqlCommand cmd = new SqlCommand(@"Insert INTO " + schemaName + "[TableLogic] (Logic_Formula,Logic_InputsCount,Logic_Inputs,Logic_ConvFactorID) VALUES (@formula, @inputscount, @inputs, @conversionfac)", con); 

    cmd.Parameters.Add("@formula", SqlDbType.NVarChar, 160).Value = textBox_Formula.Text; 
    cmd.Parameters.Add("@inputscount", SqlDbType.Int).Value = numeric_inputscount.Text; 
    cmd.Parameters.Add("@inputs", SqlDbType.NVarChar, 160).Value = textBox_Inputs.Text; 
    cmd.Parameters.Add("@conversionfac", SqlDbType.Int).Value = "(Select ConversionFactorID FROM " + schemaName + "[ConversionFactors] WHERE [conversionFactor_CF] =" + comboBox_ConfacValue.Text + " AND [ConversionFactor_Desc] = '" + combobox_conversionDescription.Text + "')"; 

    int rows = cmd.ExecuteNonQuery(); 
    MessageBox.Show("New Rule is Inserted Successfully."); 
} 
+0

對於SQL Server使用存儲過程! –

+0

最好看看如何使用ADO.NET和c#。有很多如何在那裏的文章或沿着例子。一旦你理解了如何查詢的基礎知識,然後在你的類中寫一個新的方法,可以獲取'conversionfac'的值並將其設置爲'@ conversionfac'參數的值。 IE瀏覽器。把你的問題分解成更小的部分。 – Igor

+0

@Igor我同意你的意見......我可以創建一個SQL命令來獲取「@conversionfac」的值,然後在代碼中使用它...我只是想探討一下這是唯一的方法嗎?因爲它打破了這個問題,但增加了使用的代碼行... –

回答

2

你不能。事實上,如果可以的話,那會讓你的代碼高達SQL Injection的漏洞。這也是爲什麼你不應該直接在任何SQL查詢中使用任何用戶輸入(換句話說,你使用命令參數的全部原因是爲了避免這種事情)。

您可以轉換這是一個存儲過程,而不是作爲Marciej洛杉磯suggests,但如果你正在尋找的東西快速和骯髒,這應該工作以及:

SqlCommand cmd = new SqlCommand(
    @"DECLARE @conversionfac INT;" + 
    @"SELECT @conversionfac = [ConversionFactorID] FROM " + schemaName + "[ConversionFactors] WHERE [conversionFactor_CF] = @conversionFactor_CF AND [ConversionFactor_Desc] = @combobox_conversionDesc;" + 
    @"INSERT INTO " + schemaName + "[TableLogic] (Logic_Formula,Logic_InputsCount,Logic_Inputs,Logic_ConvFactorID) VALUES (@formula, @inputscount, @inputs, @conversionfac)", con); 

cmd.Parameters.Add("@formula", SqlDbType.NVarChar, 160).Value = textBox_Formula.Text; 
cmd.Parameters.Add("@inputscount", SqlDbType.Int).Value = numeric_inputscount.Text; 
cmd.Parameters.Add("@inputs", SqlDbType.NVarChar, 160).Value = textBox_Inputs.Text; 
cmd.Parameters.Add("@conversionFactor_CF", SqlDbType.Int).Value = comboBox_ConfacValue.Text; 
cmd.Parameters.Add("@combobox_conversionDesc", SqlDbType.Int).Value = combobox_conversionDescription.Text; 
int rows = cmd.ExecuteNonQuery(); 
MessageBox.Show("New Rule is Inserted Successfully."); 
2

你不能做到這一點與您正在使用的INSERT INTO的形式。你可以使用類似下面的

string query = @"INSERT INTO tablelogic 
    (Logic_Formula,Logic_InputsCount,Logic_Inputs,Logic_ConvFactorID) 
    SELECT @formula, @inputscount, @inputs, ConversionFactorID 
    FROM conversionfactors 
    WHERE conversionfactor_cf = @cfcf and conversionfactor_desc = @cfdesc" 

SqlCommand cmd = new SqlCommand(query, con); 

cmd.Parameters.Add("@formula", SqlDbType.NVarChar, 160).Value = textBox_Formula.Text; 
cmd.Parameters.Add("@inputscount", SqlDbType.Int).Value = numeric_inputscount.Text; 
cmd.Parameters.Add("@inputs", SqlDbType.NVarChar, 160).Value = textBox_Inputs.Text; 
cmd.Parameters.Add("@cfcf", SqlDbType.Int).Value = comboBox_ConfacValue.Text; 
cmd.Parameters.Add("@cfdesc", SqlDbType.NVarChar, 160).Value = combobox_conversionDescription.Text; 

這是另一種形式的select語句,在那裏你可以選擇你的數據從另一個表中的表進行插入。有關詳細信息,請參閱this

相關問題