我不断收到异常
The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects
同时执行以下代码:
string StrQuery;
using (SqlConnection conn = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=SanFransiscoData;Integrated Security=True;Pooling=False"))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
// SqlParameter author = new SqlParameter("@author", dataGridView1.Rows[0].Cells[0].Value.ToString());
comm.Parameters.Add("@author", SqlDbType.VarChar);
comm.Parameters.Add("@title", SqlDbType.NVarChar);
comm.Parameters.Add("@genre", SqlDbType.VarChar);
comm.Parameters.Add("@price", SqlDbType.Float);
comm.Parameters.Add("@publish_date", SqlDbType.Date);
comm.Parameters.Add("@description", SqlDbType.NVarChar);
comm.Parameters.Add("@bookid", SqlDbType.VarChar);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
StrQuery = "INSERT INTO BooksData VALUES(@author,@title,@genre,@price,@publish_date,@description,@bookid)";
comm.Parameters.Add(dataGridView1.Rows[i].Cells[0].Value.ToString());
comm.Parameters.Add(dataGridView1.Rows[i].Cells[1].Value.ToString());
comm.Parameters.Add(dataGridView1.Rows[i].Cells[2].Value.ToString());
comm.Parameters.Add(Convert.ToDecimal(dataGridView1.Rows[i].Cells[3].Value));
comm.Parameters.Add(Convert.ToDateTime(dataGridView1.Rows[i].Cells[4].Value));
comm.Parameters.Add(dataGridView1.Rows[i].Cells[5].Value.ToString());
comm.Parameters.Add(dataGridView1.Rows[i].Cells[6].Value.ToString());
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}
请告诉我哪里出错了。
最佳答案
我尝试了上述所有步骤,但没有任何帮助。
终于通过更改命名空间找到并修复了它
using System.Data.SqlClient to using Microsoft.Data.SqlClient
关于c# - "SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22705767/