C# 其他信息: ExecuteNonQuery: CommandText 属性尚未初始化
//SqlConnectionsqlConn;SqlCommandcmdStr;stringsqlStr;intcount;privatevoidbutton2_Clic...
//
SqlConnection sqlConn;
SqlCommand cmdStr;
string sqlStr;
int count;
private void button2_Click(object sender, EventArgs e)
{
string strConn = "server=127.0.0.1;database=ufdata_001_2014;uid=sa;pwd=cs,./123";
sqlConn = new SqlConnection(strConn);
sqlConn.Open();
cmdStr = new SqlCommand(sqlStr,sqlConn);
for (int i = 0; i < listView1.Items.Count; i++)
{
sqlStr = "insert into Test (t_id,t_name,t_time,t_price) values (t_id = @t_id,t_name = @t_name,t_time = @t_time,t_price = @t_price)";
cmdStr.Parameters.Add("@t_id", SqlDbType.Int);
cmdStr.Parameters.Add("@t_name", SqlDbType.NVarChar,50);
cmdStr.Parameters.Add("@t_time", SqlDbType.DateTime);
cmdStr.Parameters.Add("@t_price", SqlDbType.Money);
cmdStr.Parameters["@t_id"].Value = listView1.Items[i].SubItems[0].Text;
cmdStr.Parameters["@t_name"].Value = " + listView1.Items[i].SubItems[1].Text + ";
cmdStr.Parameters["@t_time"].Value = listView1.Items[i].SubItems[2].Text;
cmdStr.Parameters["@t_price"].Value = listView1.Items[i].SubItems[3].Text;
}
if ( cmdStr.ExecuteNonQuery()> 0)
{
MessageBox.Show("新增记录成功!");
sqlConn.Close();
}
else
{
MessageBox.Show("新增失败!");
}
}
为什么提示这个,才学,不太懂,谢谢! 展开
SqlConnection sqlConn;
SqlCommand cmdStr;
string sqlStr;
int count;
private void button2_Click(object sender, EventArgs e)
{
string strConn = "server=127.0.0.1;database=ufdata_001_2014;uid=sa;pwd=cs,./123";
sqlConn = new SqlConnection(strConn);
sqlConn.Open();
cmdStr = new SqlCommand(sqlStr,sqlConn);
for (int i = 0; i < listView1.Items.Count; i++)
{
sqlStr = "insert into Test (t_id,t_name,t_time,t_price) values (t_id = @t_id,t_name = @t_name,t_time = @t_time,t_price = @t_price)";
cmdStr.Parameters.Add("@t_id", SqlDbType.Int);
cmdStr.Parameters.Add("@t_name", SqlDbType.NVarChar,50);
cmdStr.Parameters.Add("@t_time", SqlDbType.DateTime);
cmdStr.Parameters.Add("@t_price", SqlDbType.Money);
cmdStr.Parameters["@t_id"].Value = listView1.Items[i].SubItems[0].Text;
cmdStr.Parameters["@t_name"].Value = " + listView1.Items[i].SubItems[1].Text + ";
cmdStr.Parameters["@t_time"].Value = listView1.Items[i].SubItems[2].Text;
cmdStr.Parameters["@t_price"].Value = listView1.Items[i].SubItems[3].Text;
}
if ( cmdStr.ExecuteNonQuery()> 0)
{
MessageBox.Show("新增记录成功!");
sqlConn.Close();
}
else
{
MessageBox.Show("新增失败!");
}
}
为什么提示这个,才学,不太懂,谢谢! 展开
3个回答
展开全部
你这段代码由两个问题,
sqlStr没有正确传递到cmdStr对象中,这也是你的程序报错的原因。
For循环这个逻辑问题很大,具体可以看我给你的代码注释。
SqlConnection sqlConn;
SqlCommand cmdStr;
string sqlStr;
int count;
private void button2_Click(object sender, EventArgs e)
{
string strConn = "server=127.0.0.1;database=ufdata_001_2014;uid=sa;pwd=cs,./123";
sqlStr = sqlStr = "insert into Test (t_id,t_name,t_time,t_price) values (t_id = @t_id,t_name = @t_name,t_time = @t_time,t_price = @t_price)";
sqlConn = new SqlConnection(strConn); sqlConn.Open();
cmdStr = new SqlCommand(sqlStr, sqlConn);
// 这个For循环本来是想给Command对象赋值,但是你这样一循环,最后只能是ListView的最后一行插入数据库了
for (int i = 0; i < listView1.Items.Count; i++)
{
cmdStr.Parameters.Add("@t_id", SqlDbType.Int);
cmdStr.Parameters.Add("@t_name", SqlDbType.NVarChar, 50);
cmdStr.Parameters.Add("@t_time", SqlDbType.DateTime);
cmdStr.Parameters.Add("@t_price", SqlDbType.Money);
cmdStr.Parameters["@t_id"].Value = listView1.Items[i].SubItems[0].Text;
cmdStr.Parameters["@t_name"].Value = " + listView1.Items[i].SubItems[1].Text + ";
cmdStr.Parameters["@t_time"].Value = listView1.Items[i].SubItems[2].Text;
cmdStr.Parameters["@t_price"].Value = listView1.Items[i].SubItems[3].Text;
// 如果想把所有的行都插入数据库,你得把下面的cmdStr.ExecuteNoQuery()移到这里来。
// 去掉MessageBox逻辑,不然会每次都提示你。 待跳出For循环后关闭连接sqlConn.Close().
}
// 请看上面的解释
if (cmdStr.ExecuteNonQuery() > 0)
{ //MessageBox.Show("新增记录成功!");
sqlConn.Close();
}
else
{ //MessageBox.Show("新增失败!");
}
}
追问
sqlStr如何正确传入进去呢?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询