如何在表中添加多行?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您永远不会使用“)”之后的任何内容更新
command
对象的 SQL 命令文本。仅仅因为您更新了sql
变量并不意味着SqlCommand
对象会看到该更新。(您将遇到的另一个问题是您没有从此查询返回任何内容,因此您将无法使用 ExecuteScalar()。)
请尝试以下操作:
The problem is that you are never updating the SQL command text of the
command
object with anything after the ")". Just because you update thesql
variable doesn't mean thatSqlCommand
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 sql 作为引用类型,尽管它是引用类型,但它是一种特殊情况,它的行为类似于值类型。行
sql += "SELECT @title" + i + ",@count" + i + ",@price" + i + ",@date" + i + " ";
似乎是附加到
sql
,但实际上它正在创建一个新字符串,该字符串存储在内存中与传递给SqlCommand
的String
不同的位置。如果数组很大,则使用 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 linesql += "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 toString
that you passed to theSqlCommand
.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 yoursql
variable.