错误:“填充:SelectCommand.Connection属性尚未初始化." [英] Error : "Fill: SelectCommand.Connection property has not been initialized."

查看:129
本文介绍了错误:“填充:SelectCommand.Connection属性尚未初始化."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im在尝试将mysql数据库连接到编辑器时出现此错误,这是后面的代码:

im getting this error while trying to connect a mysql database to an editor, here is the code behind:

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    MySqlConnection conn = new MySqlConnection(@"connection string");//tested and working
    conn.Open();

    MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins");
    MySqlDataAdapter da = new MySqlDataAdapter(cmd);

    da.SelectCommand = cmd;
    da.Fill(dt);


    if (dt.Rows.Count > 0)
    {
        Editor1.Content = dt.Rows[0]["tes"].ToString();
    }
    conn.Close();

}

这是aspx页面代码:

and here is the aspx page code:

<body>
<form id="form1" runat="server">


<cc1:Editor ID="Editor1" runat="server" Height="400px" Visible="true" />

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>

我在做错什么,谢谢.

我正在使用asp.net 3.5.

推荐答案

更改行

MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins");

收件人

MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins", conn);

它应该可以工作.

或者将conn分配给cmd.Connection属性.

Alternatively assign conn to the cmd.Connection property.

您的代码存在问题,就是您永远不会为该命令分配连接,因此该错误表明该连接未初始化.

The problem with your code is that you never assign a connection to the command, hence the error saying that the connection is not initialized.

这篇关于错误:“填充:SelectCommand.Connection属性尚未初始化."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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