c# - SelectCommand.Connection 属性尚未初始化

标签 c# asp.net mysql datatable odbc

好的,我遇到了逻辑流程问题。出于某种原因,我声明的变量之一并没有在当前上下文中存在。错误在第 18 行:

Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.

Source Error: 


Line 16:            myAdapter.Fill(tabledata);
Line 17:        } catch (Exception ex) {
Line 18:            throw (ex);
Line 19:        } finally {
Line 20:            con.Close();

这是完整的页面源:

<% @Page Language="C#" Debug="true" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.Odbc" %>
<% @Import Namespace="System.Web.Configuration" %>
<!-- #include file="header.html" -->
<%
    string conString = WebConfigurationManager.ConnectionStrings["stampConnectionString"].ConnectionString;
    OdbcDataAdapter myAdapter = new OdbcDataAdapter();  
    DataTable tabledata = new DataTable();      
    using (OdbcConnection con = new OdbcConnection(conString)) {
        using (OdbcCommand com = new OdbcCommand("SELECT * FROM cheeseisdelicious", con)) {
            myAdapter.SelectCommand = com;
        }
        try {
            con.Open();
            myAdapter.Fill(tabledata);
        } catch (Exception ex) {
            throw (ex);
        } finally {
            con.Close();
        }
    }
    Response.Write("<table>");
    foreach (DataRow row in tabledata.Rows) {
        Response.Write("<tr>");
        foreach (var item in row.ItemArray) {
            Response.Write("<td>" + item + "</td>");
        }
        Response.Write("</tr>");
    }
    Response.Write("</table>");

%>
<!-- #include file="footer.html" -->

最佳答案

查看usings 周围的括号。内层的,就在

    myAdapter.SelectCommand = com;
} <- this one!

处理你的命令。

关于c# - SelectCommand.Connection 属性尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10657676/

相关文章:

c# - 是否有理由先转换为一个类型然后再转换为可空类型?

c# - float 范围内最大的不准确度是多少?

c# - Azure函数应用程序内部的Sftp重试逻辑

c# - 从 Controller 传递 Html 字符串以查看 ASP.Net MVC

mysql - 连接年和周函数

php - 无法从 php 连接到 MySQL "Connection Refused"

c# - 对于带有列表框的用户控件,如何将所选项目公开到父页面?

ASP.net HTTP 404 - 找不到文件而不是 MaxRequestLength 异常

c# - 如何通过webservice将动态参数传递给sql查询

php - 如何打开 .mysql 文件?