第一次我从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();
}
}
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();
}
}
复制粘贴就可以了
这通常意味着您没有设置CommandText属性,但在您的情况下,您有。
您应该尝试测试sqlQuery字符串在这一行实际上不是空的:
dataCommand.CommandText = sqlQuery;
注:作为"最佳实践",您可能需要考虑在设置SqlCommand对象之后打开连接,以最大限度地减少打开连接所花费的时间:
dataCommand.CommandType = CommandType.Text;
dataCommand.CommandText = sqlQuery;
dataCommand.Parameters.Add("@Today", DateTime.Today.ToString());
//...
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
查看您的字符串sql查询,您没有在"INTO"部分和"VALUES"部分之间留下空格。
...............Property_ID)";
sqlQuery += "VALUES (@Today, ..............
应:...............Property_ID)";
sqlQuery += " VALUES (@Today, ..............