I keep getting the exception
The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects
while executing the following code:
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();
}
}
}
Please tell me where I'm going wrong.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…