【问题标题】:How do I add multiple rows in a table?如何在表格中添加多行?
【发布时间】:2010-09-30 13:46:06
【问题描述】:
string date = p_text_data.Text;
string sql = @"INSERT INTO Warehouse (title,count,price,date) ";
try
{
    using (SqlConnection connection = ConnectToDataBase.GetConnection())
    {
        SqlCommand command = new SqlCommand(sql, connection);
        for (int i = 0; i < mdc.Count; i++)
        {
            sql += "SELECT @title" + i + ",@count" + i + ",@price" + i + ",@date" + i + " ";
            command.Parameters.AddWithValue("@title" + i, mdc[i].Title);
            command.Parameters.AddWithValue("@count" + i, mdc[i].Count);
            command.Parameters.AddWithValue("@price" + i, mdc[i].Price);
            command.Parameters.AddWithValue("@date" + i, Conver_Data(date));
            if (mdc.Count-1 != i)
                sql += "UNION ALL ";
        }
        sql += " ;";
        connection.Open();// *sql
        string id_Partner = command.ExecuteScalar().ToString();
    }
}
catch (SqlException se)
{
    MessageBox.Show(se.Message);
}

*sql = "INSERT INTO Warehouse (title,count,price,date) SELECT @title0,@count0,@price0,@date0 UNION ALL SELECT @title1,@count1,@price1,@date1 ;"

然后他飞了一个异常

')' 附近的语法不正确

澄清 - 计数 - 整数,价格 - 双精度,日期 - 日期

我做错了什么?

编辑: 表格

CREATE TABLE [dbo].[Warehouse] (
  [ID] int IDENTITY(1, 1) NOT NULL,
  [title] char(30) COLLATE Cyrillic_General_CI_AS NULL,
  [count] int NULL,
  [price] float NULL,
  [date] datetime NULL,
  CONSTRAINT [PK__Warehous__3214EC277F60ED59] PRIMARY KEY CLUSTERED ([ID])
)
ON [PRIMARY]
GO

我用的是SQL Server 2008

【问题讨论】:

  • 您使用的是什么版本的 SQL Server?
  • 该查询在 SQL Server 2008 中对我来说很好。
  • 我已经更新了我的答案

标签: c# sql-server sqlcommand


【解决方案1】:

问题是您永远不会用“)”之后的任何内容更新command 对象的SQL 命令文本。仅仅因为您更新了sql 变量并不意味着SqlCommand 对象将会看到该更新。

(您将遇到的另一个问题是您没有从该查询中返回任何内容,因此您将无法使用ExecuteScalar()。)

试试这个:

string date = p_text_data.Text; 
string sql = @"INSERT INTO Warehouse (title,count,price,date) "; 
try 
{ 
    using (SqlConnection connection = ConnectToDataBase.GetConnection()) 
    { 
        SqlCommand command = new SqlCommand(sql, connection); 
        for (int i = 0; i < mdc.Count; i++) 
        { 
            sql += "SELECT @title" + i + ",@count" + i + ",@price" + i + ",@date" + i + " "; 
            command.Parameters.AddWithValue("@title" + i, mdc[i].Title); 
            command.Parameters.AddWithValue("@count" + i, mdc[i].Count); 
            command.Parameters.AddWithValue("@price" + i, mdc[i].Price); 
            command.Parameters.AddWithValue("@date" + i, Conver_Data(date)); 
            if (mdc.Count-1 != i) 
                sql += "UNION ALL "; 
        } 
        sql += " ;"; 
        command.CommandText = sql;    //  Set your SQL Command to the whole statement.
        connection.Open();// *sql 
        command.ExecuteNonQuery();    //  Execute a query with no return value.
    } 
} 
catch (SqlException se) 
{ 
    MessageBox.Show(se.Message); 
} 

【讨论】:

    【解决方案2】:

    您正在尝试将 String sql 用作引用类型,尽管它是引用类型,但它是一种特殊情况,它的作用类似于值类型。线

    sql += "SELECT @title" + i + ",@count" + i + ",@price" + i + ",@date" + i + " ";

    似乎附加到sql,但实际上它正在创建一个新字符串,存储在内存中与您传递给SqlCommandString不同的位置。

    如果您的数组很大,您可能会看到通过使用 StringBuilder 类来构建您的字符串,然后在构建后将其分配给您的 SqlCommand 对象来获得性能优势。

    sql 变量中包含完整的 SQL 之后,您需要将其分配给 SqlCommand.CommandText

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-16
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 2012-02-11
      • 2020-12-08
      • 2019-08-10
      相关资源
      最近更新 更多