[求助]关于DataSet的使使及DataGrid的数据绑定!
就是下面加粗的那行代码出错,可能是不能创建DataSet对象,
我初学,这是按书上实例照抄的,但不知是哪里出了错,看VS的联机帮助也没找到答案,所以向各网友求助.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
我初学,这是按书上实例照抄的,但不知是哪里出了错,看VS的联机帮助也没找到答案,所以向各网友求助.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace AdoNetBase
{
/// <summary>
/// DataSet 的摘要说明。
/// </summary>
public class DataSet : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid myDataGrid;
private static String SqlConnectionString = @"Data Source = Datacenter\sql2000;" +
"Initial Catalog = Northwind; Integrated Security = SSPI Persist Security Info = False; Workstation Id = DATACENTER";
{
/// <summary>
/// DataSet 的摘要说明。
/// </summary>
public class DataSet : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid myDataGrid;
private static String SqlConnectionString = @"Data Source = Datacenter\sql2000;" +
"Initial Catalog = Northwind; Integrated Security = SSPI Persist Security Info = False; Workstation Id = DATACENTER";
private void CreateDataSetObject()
{
SqlConnection myConnection = new SqlConnection(SqlConnectionString);
String cmdText = "SELECT TOP 8 * FROM Customers";
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
myConnection.Open();
{
SqlConnection myConnection = new SqlConnection(SqlConnectionString);
String cmdText = "SELECT TOP 8 * FROM Customers";
SqlCommand myCommand = new SqlCommand(cmdText,myConnection);
myConnection.Open();
DataTable table = new DataTable("Cusomers");
// 构造DataTable的DataColumn
table.Columns.Add("Index");
table.Columns.Add("CustomerID");
table.Columns.Add("CompanyName");
table.Columns.Add("ContactName");
// 构造DataTable的DataColumn
table.Columns.Add("Index");
table.Columns.Add("CustomerID");
table.Columns.Add("CompanyName");
table.Columns.Add("ContactName");
SqlDataReader CustReader = myCommand.ExecuteReader(); //从数据库中读取数据
int index = 0;
while(CustReader.Read())
{
DataRow row = table.NewRow();
row["Index"] = (++index).ToString();
row["CustomerID"] = CustReader["CustomerID"];
row["CompanyName"] = CustReader["CompanyName"];
row["ContactName"] = CustReader["ContactName"];
table.Rows.Add(row);
}
CustReader.Close();
myConnection.Close();
myDataGrid.DataSource = new DataSet(table);
myDataGrid.DataBind();
}
int index = 0;
while(CustReader.Read())
{
DataRow row = table.NewRow();
row["Index"] = (++index).ToString();
row["CustomerID"] = CustReader["CustomerID"];
row["CompanyName"] = CustReader["CompanyName"];
row["ContactName"] = CustReader["ContactName"];
table.Rows.Add(row);
}
CustReader.Close();
myConnection.Close();
myDataGrid.DataSource = new DataSet(table);
myDataGrid.DataBind();
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
CreateDataSetObject();
}
{
if(!Page.IsPostBack)
CreateDataSetObject();
}