gpt4 book ai didi

C# 数据库插入 (ASP.NET) - ExecuteNonQuery : CommandText property has not been initialized

转载 作者:搜寻专家 更新时间:2023-10-30 19:58:24 31 4
gpt4 key购买 nike

我第一次从 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();
}
}

复制粘贴就可以了

关于C# 数据库插入 (ASP.NET) - ExecuteNonQuery : CommandText property has not been initialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5838511/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com