gpt4 book ai didi

c# - 如何将 DataTable 从类传递到页面

转载 作者:太空宇宙 更新时间:2023-11-03 14:10:31 24 4
gpt4 key购买 nike

我正在学习 C# 中的类,并正在尝试构建“DataAccessClass”。我有一个有效的“OpenSqlConnection”void,并将其用作构建“OpenSqlDatareader”void 的指南。我正在尝试合并我在这个类中的所有数据库调用。我的困惑是如何将 DataTable 从类传递到 aspx 页面以将其与 ListView 绑定(bind)。我一直在阅读并尝试了一天来完成这项工作,但无法弄清楚我做错了什么......

这是 Page_Load:

// --------------------------------------------------------------------------------
// Populate the Customers page.
// --------------------------------------------------------------------------------


// Define the query
string sqlQuery = " SELECT CustomerID, LastName + ', ' + FirstName AS CustomerName, Email, City, State, Phone"
+ " FROM Customer"
+ " ORDER BY LastName, FirstName";

string strErrorMessage = "";

if (DataAccessClass.OpenSqlConnection(out strErrorMessage) == false)
{
string strErrorType = "Database Connection Error:";
SendErrorMessageToClient(strErrorType, strErrorMessage);
}

else if (DataAccessClass.OpenSqlDatareader(sqlQuery, out dt, out strErrorMessage) == false)
{
string strErrorType = "Datareader Error:";
SendErrorMessageToClient(strErrorType, strErrorMessage);
}

else

{
// Bind the Listview
lvCustomers.DataSource = dt;
lvCustomers.DataBind();
dt.Dispose();
}

这是类(class):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for DataAccessClass
/// </summary>
///
public class DataAccessClass
{
public DataAccessClass()
{
//
// TODO: Add constructor logic here
//
}



// -----------------------------------------------------------------------------------------
// Name: OpenSqlConnection
// Abstract: Open a connection to a SQL Server
// -----------------------------------------------------------------------------------------
public static bool OpenSqlConnection(out string strErrorMessage)
{

SqlConnection theConnection = new SqlConnection();
bool blnResult = false;
string strConnectionString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
strErrorMessage = "";

if (theConnection == null)
theConnection = new SqlConnection();

try
{
switch (theConnection.State)
{
case ConnectionState.Broken:
theConnection.Close();
theConnection.ConnectionString = strConnectionString;
theConnection.Open();
blnResult = true;
break;

case ConnectionState.Closed:
theConnection.ConnectionString = strConnectionString;
theConnection.Open();
blnResult = true;
break;

case ConnectionState.Open:
blnResult = true;
break;

default:
strErrorMessage = "Connection state is " + theConnection.State.ToString();
break;
}
}
catch (Exception excError)
{
strErrorMessage = excError.Message;
}

return blnResult;
}

// -----------------------------------------------------------------------------------------
// Name: OpenSqlDataReader
// Abstract: Open a SQL DataReader
// -----------------------------------------------------------------------------------------
public static bool OpenSqlDatareader(string sqlQuery, out dt, out string strErrorMessage)
{

SqlConnection theConnection = new SqlConnection();

bool blnResult = false;
string strConnectionString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
strErrorMessage = "";

if (theConnection == null)
theConnection = new SqlConnection();

try
{
// Declare a SQL Adapter
SqlDataAdapter da = new SqlDataAdapter(sqlQuery, theConnection);

// Declare a DataTable
DataTable dt = new DataTable();

// Populate the DataTable
da.Fill(dt);

// Clean up.
dt.Dispose();
da.Dispose();
theConnection.Close();
}
catch (Exception excError)
{
strErrorMessage = excError.Message;
}

return blnResult;
}
}

错误消息是:“找不到类型或命名空间名称‘dt’(是否缺少 using 指令或程序集引用?)”有什么建议吗?

最佳答案

要添加到此代码的问题列表中(您还没有发现,因为您还没有走那么远):

在您的 OpenSqlDataReader 方法中,您需要使用您定义的连接字符串实例化您的 SqlConnection 对象。现在您只是使用空构造函数创建它,这意味着它不会用作实际的连接对象。

此外,您还需要在连接对象上显式调用 .Open()。否则,它不会打开,并且当您尝试使用它时会抛出异常。

关于c# - 如何将 DataTable 从类传递到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8014777/

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