Oracle 11g数据库连接正常,但GridView中没有显示数据,请帮忙! 以下代码位于页面加载代码中
OracleConnection con = new OracleConnection();
con.ConnectionString = "DATA SOURCE=DEMODB;USER ID=SYSTEM;password=admin";
con.Open();
string sql = "select * from Insurance";
OracleCommand comm = new OracleCommand(sql, con);
OracleDataAdapter da = new OracleDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds, "Insurance");
GridView1.DataSource = ds.Tables["Insurance"];
GridView1.DataBind();
con.Close();
这是页面的源代码:
</asp:Label>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:SqlDataSource ID="DataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT "SSN", "RID", "EID", "TYPE", "COST" FROM "TREATMENT"">
</asp:SqlDataSource>
</div>
答案 0 :(得分:0)
尝试使用此代替Page_Load
代码并完全删除<asp:SqlDataSource
。
DataTable results = new DataTable();
using (OracleConnection con = new OracleConnection("DATA SOURCE=DEMODB;USER ID=SYSTEM;password=admin") {
con.Open();
using (OracleCommand comm = new OracleCommand("select * from Insurance", con)) {
results.Load(comm.ExecuteReader());
}
}
GridView1.DataSource = results;
GridView1.DataBind();
GridView应该简单:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>