如何在SQL commnad中使用cmd参数中的子查询?

问题描述:

我有下面显示的代码,我试图根据用户输入插入某些值到数据库中。我需要验证用户输入,这就是为什么我使用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”的值,然后在代码中使用它...我只是想探讨一下这是唯一的方法吗?因为它打破了这个问题,但增加了使用的代码行... –

你不能。事实上,如果可以的话,那会让你的代码高达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."); 

你不能做到这一点与您正在使用的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