gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:52:06 25 4
gpt4 key购买 nike

好的,我遇到了逻辑流程问题。出于某种原因,我声明的变量之一并没有在当前上下文中存在。错误在第 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/

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