[英]C# Database Insert (ASP.NET) - ExecuteNonQuery: CommandText property has not been initialized
first time I'm doing an insert from ASP.NET/C# and I'm having a little issue.我第一次从 ASP.NET/C# 插入,我遇到了一个小问题。 I keep getting the following error every time this code runs: " ExecuteNonQuery: CommandText property has not been initialized" Does anyone know what this means and how I fix it?每次运行此代码时,我都会收到以下错误:“ ExecuteNonQuery:CommandText 属性尚未初始化” 有谁知道这意味着什么以及我如何解决它?
Thanks in advance!提前致谢!
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();
}
}
Copy-paste should do the trick复制粘贴应该可以解决问题
This usually means you haven't set the CommandText property, but in your case, you have.这通常意味着您没有设置 CommandText 属性,但在您的情况下,您已经设置了。
You should try testing that the sqlQuery string is actually not empty at this line:您应该尝试在这一行测试 sqlQuery 字符串实际上不为空:
dataCommand.CommandText = sqlQuery;
PS As a "best practice", you may want to consider opening the connection AFTER setting up the SqlCommand object, to minimize the time spent with an open connection: PS 作为“最佳实践”,您可能需要考虑在设置 SqlCommand object 之后打开连接,以最大限度地减少打开连接所花费的时间:
dataCommand.CommandType = CommandType.Text;
dataCommand.CommandText = sqlQuery;
dataCommand.Parameters.Add("@Today", DateTime.Today.ToString());
//...
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
Looking at your string sql query, you're not leaving a space between the "INTO" part and "VALUES" part.查看您的字符串 sql 查询,您不会在“INTO”部分和“VALUES”部分之间留下空格。
...............Property_ID)";
sqlQuery += "VALUES (@Today, ..............
SHOULD BE:应该:
...............Property_ID)";
sqlQuery += " VALUES (@Today, ..............
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.