C#数据库插入(ASP.NET) - 的ExecuteNonQuery:CommandText属性尚未初始化 [英] C# Database Insert (ASP.NET) - ExecuteNonQuery: CommandText property has not been initialized

查看:3827
本文介绍了C#数据库插入(ASP.NET) - 的ExecuteNonQuery:CommandText属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次做从ASP.NET/C#插入,我有一个小问题。每次我不断收到以下错误此code运行的ExecuteNonQuery:CommandText属性尚未初始化?有谁知道这是什么意思,我该如何解决这个问题。

在此先感谢!

 字符串的SQLQuery =INSERT INTO ATI_LOG_IO(日期,CONNECT_TIME,Disconnect_Time,ATI_Rep,Reason_For_Access,Property_Contact,CASE_NUMBER,评论,PROPERTY_ID);
的SQLQuery + =VALUES(@Today,@Connect,@Disconnect,@Rep,@Reason,@contact,@CaseNum,@Comments,@PropertyID);
使用(SqlConnection的dataConnection =新的SqlConnection(的connectionString))
{
    使用(的SqlCommand dataCommand = dataConnection.CreateCommand())
    {
        dataConnection.Open();
        dataCommand.CommandType = CommandType.Text;
        dataCommand.CommandText =的SQLQuery;
        dataCommand.Parameters.Add(@今日,DateTime.Today.ToString());
        dataCommand.Parameters.Add(@连接,txtInDate.Text ++ fromHrs.Text +:+ fromMins.Text +:00);
        dataCommand.Parameters.Add(@断开,txtOutdate.Text ++ toHrs.Text +:+ fromMins.Text +:00);
        dataCommand.Parameters.Add(@ REP,REPID);
        dataCommand.Parameters.Add(@理,txtReason.Text);
        dataCommand.Parameters.Add(@联系人,txtContact.Text);
        dataCommand.Parameters.Add(@ CaseNum,txtCaseNum.Text);
        dataCommand.Parameters.Add(@评论,txtComments.Text);
        dataCommand.Parameters.Add(@物业ID,lstProperties.SelectedValue);
        dataCommand.ExecuteNonQuery();
        dataConnection.Close();
    }
}


解决方案

 字符串的SQLQuery =INSERT INTO ATI_LOG_IO(日期,CONNECT_TIME,Disconnect_Time,ATI_Rep,Reason_For_Access,Property_Contact,CASE_NUMBER,评论, PROPERTY_ID);
的SQLQuery + =VALUES(@Today,@Connect,@Disconnect,@Rep,@Reason,@contact,@CaseNum,@Comments,@PropertyID);
使用(SqlConnection的dataConnection =新的SqlConnection(的connectionString))
{
    使用(的SqlCommand dataCommand =新的SqlCommand(sqlquery的,dataConnection))
    {
        dataCommand.Parameters.AddWithValue(今日,DateTime.Today.ToString());
        dataCommand.Parameters.AddWithValue(连接,txtInDate.Text ++ fromHrs.Text +:+ fromMins.Text +:00);
        dataCommand.Parameters.AddWithValue(断开,txtOutdate.Text ++ toHrs.Text +:+ fromMins.Text +:00);
        dataCommand.Parameters.AddWithValue(REP,REPID);
        dataCommand.Parameters.AddWithValue(理,txtReason.Text);
        dataCommand.Parameters.AddWithValue(联系方式,txtContact.Text);
        dataCommand.Parameters.AddWithValue(CaseNum,txtCaseNum.Text);
        dataCommand.Parameters.AddWithValue(注释,txtComments.Text);
        dataCommand.Parameters.AddWithValue(物业ID,lstProperties.SelectedValue);        dataConnection.Open();
        dataCommand.ExecuteNonQuery();
        dataConnection.Close();
    }
}

复制粘贴应该做的伎俩

first time I'm doing an insert from ASP.NET/C# and I'm having a little issue. 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?

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

这篇关于C#数据库插入(ASP.NET) - 的ExecuteNonQuery:CommandText属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆