【问题标题】:How to use subquery inside cmd parameters in SQL commnad?如何在 SQL 命令的 cmd 参数中使用子查询?
【发布时间】:2016-04-26 17:23:35
【问题描述】:

我有下面显示的代码,我试图根据用户输入将某些值插入数据库。我需要验证用户输入,这就是我使用 SQL 命令进行插入的原因。

我可以使用 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.");
}

【问题讨论】:

  • 对于 SQL Server 使用存储过程!
  • 最好看看如何使用 ADO.NET 和 c#。有很多如何撰写文章或遵循示例。一旦您了解了如何查询的基础知识,然后在您的类中编写一个新方法,该方法可以获取 conversionfac 的值并将其设置为 @conversionfac 参数的值。 IE。把你的问题分解成更小的部分。
  • @Igor 我同意你的 cmets...我可以创建一个 SQL 命令来获取“@conversionfac”的值,然后在代码中使用它...我只是想探索一下唯一的办法 ?因为它打破了问题,但增加了使用的代码行......
  • @GowthamRamamoorthy - 你可以,但你必须在命令文本中而不是在参数中这样做。参数总是单值或本身代表一个表,但永远不能是要执行的表达式/sql。

标签: c# .net visual-studio-2012 windows-applications sqlcommand


【解决方案1】:

你不能。事实上,如果可以的话,那会让你的代码面临大量的SQL Injection 漏洞。这也是为什么您不应该在任何 SQL 查询中直接使用任何用户输入的原因(换句话说,您使用命令参数的全部原因是为了避免这种事情)。

您可以将其转换为存储过程,而不是像 Marciej Los 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

    【讨论】:

      猜你喜欢
      • 2013-03-04
      • 1970-01-01
      • 2017-05-08
      • 2020-07-20
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多