【问题标题】:SQL Insert statement syntaxSQL 插入语句语法
【发布时间】:2023-03-17 23:50:01
【问题描述】:

基本上我所做的是,在单击按钮时,程序将根据用户选择的内容从特定行中提取数据,并使用INSERT 将其放置在不同的表中。以下是代码。

private void button3_Click(object sender, EventArgs e)
{
        const String connectionString = "Data Source = Vanessa-PC\\SQLEXPRESS; Initial Catalog = IUMDC; Connect Timeout = 15; Integrated Security = true";
        SqlConnection con = new SqlConnection(connectionString);

        //int SituationID1; 
        label24.Show();

        foreach (SitID x in Sittbl)
        {
            if (x.ID == Convert.ToInt16(comboBox1.SelectedItem))
            {
                try
                {
                    con.Open();
                    SqlCommand command = new SqlCommand("SELECT * FROM Situation WHERE SituationID=" + x.SitIDs, con);
                    SqlDataReader dr = command.ExecuteReader();

                    while (dr.Read())
                    {
                        sitid1 = Convert.ToInt32(dr[0]);
                        name1 = dr[4].ToString();
                        incident1 = Convert.ToDateTime(dr[1]);
                        charges1 = dr[5].ToString();
                        nature1 = dr[2].ToString();
                    }

                    con.Close();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Database failed to connect" + ex.Message);
                }

                //SituationID = x.SitIDs;
            }
        }

        try
        {                       
           con.Open();
           SqlCommand command1 = new SqlCommand("INSERT INTO CurrentSit VALUES (" + sitid1 + ",'" + incident1.ToString("YYYY-mm-DD") + "', '" + nature1 + "', '" + name1 + "', '" + charges1 + "'", con);
           SqlDataReader dr1 = command1.ExecuteReader();
           con.Close();
        }
        catch (SqlException ex)
        {
            MessageBox.Show("Database failed to connect" + ex.Message);
        }
        //Situation Sit = new Situation();
        //Sit.ShowDialog();
    }

我的代码失败并说

行项目附近的语法不正确”

两个表的类型相同,我已经尝试过彻底测试!

【问题讨论】:

  • 在执行INSERT 时,您不会返回数据集合-因此,您应该使用command1.ExecuteNonQuery(); 而不是.ExecuteReader(),然后您就可以忽略读者了,真的.. ..

标签: c# sql .net sql-server insert


【解决方案1】:

看起来您的最后一条 sql 语句不正确。也许原因是在你的 sql 语句中使用了撇号,但你不应该关心它。我在回答的中间解释了为什么你不应该关心。

SqlCommand command1 = new SqlCommand("INSERT INTO CurrentSit VALUES (" + sitid1 + ",'" + incident1.ToString("YYYY-mm-DD") + "', '" + nature1 + "', '" + name1 + "', '" + charges1 + "'", con);

要找出究竟是什么问题,您可以指定列名。但我建议您使用 参数化查询 甚至不需要指定列名。

SqlCommand command1 = new SqlCommand("INSERT INTO CurrentSit VALUES(@sitid1, @incident1, @nature1, @name1, @charges1)", con);

command1.Parameters.AddWithValue("@sitid1", sitid1);
command1.Parameters.AddWithValue("@incident1", incident1.ToString("YYYY-mm-DD"));
command1.Parameters.AddWithValue("@nature1", nature1);
command1.Parameters.AddWithValue("@name1", name1);
command1.Parameters.AddWithValue("@charges1", charges1);

command1.ExecuteNonQuery();

您应该始终使用 parameterized queries。此类代码对SQL Injection 攻击开放。

和 Marc mentioned 一样,这个 sql 语句没有必要使用 ExecuteReader(),因为它只是 INSERT 数据,不返回任何数据。因此,在这种情况下,您只需要使用ExecuteNonQuery()

【讨论】:

  • 你不知道我现在有多感激!非常感谢!
【解决方案2】:

你的直接问题是你的INSERT 语句有一些无效的语法;您用来构建它的值之一很可能有一个撇号,它会提前终止您的字符串。

您更大的问题是您永远不应该以这种方式构建插入语句。除了极易出现您所看到的那种错误之外,它也是 SQL 注入攻击的典型开端。 (例如,想象一下情况的性质foo'); drop table situation; --)。

您应该使用参数化查询:

var sql = "INSERT INTO CurrentSit VALUES (@sitid, @incident, @nature, @name, @charges)"
var cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@Sitid", SqlDbType.Int).Value = sitid1;
// etc.

【讨论】:

    【解决方案3】:

    尝试在查询中指定列:

    "INSERT INTO CurrentSit (sitid, incident, nature, name1, charges) VALUES (" + sitid1 + ",'" + incident1.ToString("YYYY-mm-DD") + "', '" + nature1 + "', '" + name1 + "', '" + charges1 + "'", con)";
    

    作为附加说明,还要学习使用参数化查询。例如:

    command1.Parameters.AddWithValue("@name1", name1);  
    command1.Parameters.AddWithValue("@charges1", charges1); 
    

    http://johnhforrest.com/2010/10/parameterized-sql-queries-in-c/

    【讨论】:

      猜你喜欢
      • 2014-05-25
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多