C#数据库插入(ASP.NET) - ExecuteNonQuery:CommandText属性尚未初始化

Dav*_*rry 2 c# sql database asp.net

我第一次从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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Dim*_*tri 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

复制粘贴应该可以解决问题