我第一次从ASP.NET / C#进行插入,我遇到了一些问题。每次运行此代码时,我都会收到以下错误:“ExecuteNonQuery:CommandText属性尚未初始化”有谁知道这意味着什么以及我如何修复它?
提前致谢!
string sqlQuery = "INSERT INTO ATI_LOG_IO (Date, Connect_Time, Disconnect_Time, ATI_Rep, Reason_For_Access, Property_Contact, Case_Number, Comments, Property_ID)";
sqlQuery += "VALUES (@Today, @Connect, @Disconnect, @Rep, @Reason, @Contact, @CaseNum, @Comments, @PropertyID)";
using (SqlConnection dataConnection = new SqlConnection(connectionString))
{
using (SqlCommand dataCommand = dataConnection.CreateCommand())
{
dataConnection.Open();
dataCommand.CommandType = CommandType.Text;
dataCommand.CommandText = sqlQuery;
dataCommand.Parameters.Add("@Today", DateTime.Today.ToString());
dataCommand.Parameters.Add("@Connect", txtInDate.Text + " " + fromHrs.Text + ":" + fromMins.Text + ":00");
dataCommand.Parameters.Add("@Disconnect", txtOutdate.Text + " " + toHrs.Text + ":" + fromMins.Text + ":00");
dataCommand.Parameters.Add("@Rep", repID);
dataCommand.Parameters.Add("@Reason", txtReason.Text);
dataCommand.Parameters.Add("@Contact", txtContact.Text);
dataCommand.Parameters.Add("@CaseNum", txtCaseNum.Text);
dataCommand.Parameters.Add("@Comments", txtComments.Text);
dataCommand.Parameters.Add("@PropertyID", lstProperties.SelectedValue);
dataCommand.ExecuteNonQuery();
dataConnection.Close();
}
}
答案 0 :(得分:6)
string sqlQuery = "INSERT INTO ATI_LOG_IO (Date, Connect_Time, Disconnect_Time, ATI_Rep, Reason_For_Access, Property_Contact, Case_Number, Comments, Property_ID)";
sqlQuery += " VALUES (@Today, @Connect, @Disconnect, @Rep, @Reason, @Contact, @CaseNum, @Comments, @PropertyID)";
using (SqlConnection dataConnection = new SqlConnection(connectionString))
{
using (SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection))
{
dataCommand.Parameters.AddWithValue("Today", DateTime.Today.ToString());
dataCommand.Parameters.AddWithValue("Connect", txtInDate.Text + " " + fromHrs.Text + ":" + fromMins.Text + ":00");
dataCommand.Parameters.AddWithValue("Disconnect", txtOutdate.Text + " " + toHrs.Text + ":" + fromMins.Text + ":00");
dataCommand.Parameters.AddWithValue("Rep", repID);
dataCommand.Parameters.AddWithValue("Reason", txtReason.Text);
dataCommand.Parameters.AddWithValue("Contact", txtContact.Text);
dataCommand.Parameters.AddWithValue("CaseNum", txtCaseNum.Text);
dataCommand.Parameters.AddWithValue("Comments", txtComments.Text);
dataCommand.Parameters.AddWithValue("PropertyID", lstProperties.SelectedValue);
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
}
}
复制粘贴应该可以解决问题
答案 1 :(得分:1)
这通常意味着你没有设置CommandText属性,但在你的情况下,你有。
您应该尝试在此行测试sqlQuery字符串实际上不为空:
dataCommand.CommandText = sqlQuery;
P.S。作为“最佳实践”,您可能需要考虑在设置SqlCommand对象后打开连接,以最大限度地减少打开连接所花费的时间:
dataCommand.CommandType = CommandType.Text;
dataCommand.CommandText = sqlQuery;
dataCommand.Parameters.Add("@Today", DateTime.Today.ToString());
//...
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
答案 2 :(得分:1)
查看你的字符串sql查询,你不会在“INTO”部分和“VALUES”部分之间留下空格。
...............Property_ID)";
sqlQuery += "VALUES (@Today, ..............
应该是:
...............Property_ID)";
sqlQuery += " VALUES (@Today, ..............