如何在表中添加多行?

发布于 2024-09-25 05:55:56 字数 1620 浏览 1 评论 0原文

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 ;"

然后他飞了一个例外

“)”附近的语法不正确

澄清 - 计数 - int、价格 - double、日期 - 日期

我做错了什么?

编辑: 表SQL Server 2008

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

我使用的

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 ;"

Then he flies an exception

Incorrect syntax near ')'

clarify - count - int, price - double, date - Date

what am I doing wrong?

edit:
Table

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

I'm used SQL Server 2008

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看海 2024-10-02 05:55:56

问题是您永远不会使用“)”之后的任何内容更新 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); 
} 

The problem is that you are never updating the SQL command text of the command object with anything after the ")". Just because you update the sql variable doesn't mean that SqlCommand object is going to see that update.

(Another problem that you will run into is that you are not returning anything from this query, so you won't be able to use ExecuteScalar().)

Try this instead:

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); 
} 
朦胧时间 2024-10-02 05:55:56

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

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

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

如果数组很大,则使用 StringBuilder 可能会带来性能优势 类来构建字符串,然后在构建后将其分配给 SqlCommand 对象。

无论哪种方式,在 sql 变量中拥有完整的 SQL 后,您都需要将其分配给 SqlCommand.CommandText

You are trying to use the String sql as a reference type, although it is a reference type, it is a special case where it acts like a value type. The line

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

appears to be appending to sql but in fact it is creating a new string, stored in a different location in memory to String that you passed to the SqlCommand.

If you array is large you may see a performance benefit by using the StringBuilder class to build your string then assign it to your SqlCommand object after it has been built.

Either way you need to assign it to SqlCommand.CommandText after you have the complete SQL in your sql variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文