<%@Page Language="C#"%><%@Import Namespace="System.Data"%> <%@Import Namespace="System.Data.Common"%> <%@Import Namespace="System.Data.OleDb"%><%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head> <title>The .NET DataSet and OleDbDataAdapter Objects</title> <!-- #include file="..\global\style.inc" --> </head> <body bgcolor="#ffffff"> <span class="heading">The .NET DataSet and OleDbDataAdapter Objects</span><hr /> <!---------------------------------------------------------------------------><%-- insert connection string script --%> <wrox:connect id="ctlConnectStrings" runat="server"/><div>Connection string: <b><span id="outConnect" runat="server"></span></b></div> <div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div> <div id="outError" runat="server">+nbsp;</div><asp:datagrid id="dgrResult" runat="server" /><script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e) { // get connection string from ..\global\connect-strings.ascx user control string strConnect = ctlConnectStrings.OLEDBConnectionString; outConnect.InnerText = strConnect; // and display it // specify the SELECT statement to extract the data string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861003%'"; outSelect.InnerText = strSelect; // and display it // declare a variable to hold a DataSet object // note that we have to create it outside the Try..Catch block // as this is a separate block and so is a different scope DataSet objDataSet = new DataSet(); try { // create a new Connection object using the connection string OleDbConnection objConnect = new OleDbConnection(strConnect); // create a new DataAdapter using the connection object and select statement OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSelect, objConnect); // fill the dataset with data from the DataAdapter object objDataAdapter.Fill(objDataSet, "Books"); } catch (Exception objError) { // display error details outError.InnerHtml = "<b>* Error while accessing data</b>.<br />" + objError.Message + "<br />" + objError.Source; return; // and stop execution } // create a DataView object for the Books table in the DataSet DataView objDataView = new DataView(objDataSet.Tables["Books"]); // assign the DataView object to the DataGrid control dgrResult.DataSource = objDataView; dgrResult.DataBind(); // and bind (display) the data }
DataReader可以作为DataSource直接绑定到控件上,但是不可以填充DataSet。 <%@Page Language="C#" %><%@Import Namespace="System.Data" %> <%@Import Namespace="System.Data.OleDb" %><%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head> <title>Repeated-Value Data Binding to a DataReader Object</title> <style type="text/css"> body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt} input {font-family:Tahoma,Arial,sans-serif; font-size:9pt} .heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold} .subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px} .cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt} </style></head> <body bgcolor="#ffffff"> <span class="heading">Repeated-Value Data Binding to a DataReader Object</span><hr /> <!---------------------------------------------------------------------------><%-- insert connection string script --%> <wrox:connect id="ctlConnectStrings" runat="server"/><div id="outError" runat="server" /><!-- Unlike the DataView we can only bind a object DataReader to one control --> <!-- after it has bound the data the reader is at the end of the source rowset --> <b><ASP:DataGrid></b> control:<br /> <ASP:DataGrid id="MyDataGrid" runat="server" /><p /><!---------------------------------------------------------------------------><script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e) { // get connection string from ..\global\connect-strings.ascx user control string strConnect = ctlConnectStrings.OLEDBConnectionString; // create a SQL statement to select some rows from the database string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%1861003%'"; // create a variable to hold an instance of a DataReader object OleDbDataReader objDataReader; try { // create a new Connection object using the connection string OleDbConnection objConnect = new OleDbConnection(strConnect); // open the connection to the database objConnect.Open(); // create a new Command using the connection object and select statement OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect); // execute the SQL statement against the command to get the DataReader objDataReader = objCommand.ExecuteReader(); } catch (Exception objError) { // display error details outError.InnerHtml = "<b>* Error while accessing data</b>.<br />" + objError.Message + "<br />" + objError.Source + "<p />"; return; // and stop execution } // set the DataSource property of the control // a DataGrid can figure out the columns in the DataReader // by itself, so we just set the DataSource property MyDataGrid.DataSource = objDataReader; MyDataGrid.DataBind(); // and bind the control }</script><!---------------------------------------------------------------------------> <!-- #include file="..\global\foot.inc" --> </body> </html>
是用的sql示例,和ole差不多 private void FillDataSet() { SqlConnection cn = new SqlConnection(); strConn = "Provider=msdaora;Data Source=robin;User Id=cqsiyo;Password=robinsoft;"; strSql = "Select * from tree"; cn.ConnectionString = strConn; cn.Open(); SqlDataAdapter cmd = new SqlDataAdapter(strSql,cn); DataSet ds = new DataSet(); cmd.Fill(ds); cn.Close(); }
SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds, "作者");
dr.Fill(ds,"表名");一定要加个表名哦
如何办,我加了,还是不行。
DataSet ds=new DataSet();
dr.Fill(ds,"tree");
<%@Import Namespace="System.Data.Common"%>
<%@Import Namespace="System.Data.OleDb"%><%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>The .NET DataSet and OleDbDataAdapter Objects</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">The .NET DataSet and OleDbDataAdapter Objects</span><hr />
<!---------------------------------------------------------------------------><%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/><div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div>
<div id="outError" runat="server">+nbsp;</div><asp:datagrid id="dgrResult" runat="server" /><script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e)
{
// get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString;
outConnect.InnerText = strConnect; // and display it // specify the SELECT statement to extract the data
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861003%'";
outSelect.InnerText = strSelect; // and display it // declare a variable to hold a DataSet object
// note that we have to create it outside the Try..Catch block
// as this is a separate block and so is a different scope
DataSet objDataSet = new DataSet(); try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect); // create a new DataAdapter using the connection object and select statement
OleDbDataAdapter objDataAdapter = new OleDbDataAdapter(strSelect, objConnect); // fill the dataset with data from the DataAdapter object
objDataAdapter.Fill(objDataSet, "Books");
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
} // create a DataView object for the Books table in the DataSet
DataView objDataView = new DataView(objDataSet.Tables["Books"]); // assign the DataView object to the DataGrid control
dgrResult.DataSource = objDataView;
dgrResult.DataBind(); // and bind (display) the data }
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
<%@Page Language="C#" %><%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %><%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Repeated-Value Data Binding to a DataReader Object</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:9pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
</style></head>
<body bgcolor="#ffffff">
<span class="heading">Repeated-Value Data Binding to a DataReader Object</span><hr />
<!---------------------------------------------------------------------------><%-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/><div id="outError" runat="server" /><!-- Unlike the DataView we can only bind a object DataReader to one control -->
<!-- after it has bound the data the reader is at the end of the source rowset -->
<b><ASP:DataGrid></b> control:<br />
<ASP:DataGrid id="MyDataGrid" runat="server" /><p /><!---------------------------------------------------------------------------><script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e)
{
// get connection string from ..\global\connect-strings.ascx user control
string strConnect = ctlConnectStrings.OLEDBConnectionString; // create a SQL statement to select some rows from the database
string strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '%1861003%'"; // create a variable to hold an instance of a DataReader object
OleDbDataReader objDataReader; try
{
// create a new Connection object using the connection string
OleDbConnection objConnect = new OleDbConnection(strConnect); // open the connection to the database
objConnect.Open(); // create a new Command using the connection object and select statement
OleDbCommand objCommand = new OleDbCommand(strSelect, objConnect); // execute the SQL statement against the command to get the DataReader
objDataReader = objCommand.ExecuteReader();
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing data</b>.<br />"
+ objError.Message + "<br />" + objError.Source + "<p />";
return; // and stop execution
}
// set the DataSource property of the control
// a DataGrid can figure out the columns in the DataReader
// by itself, so we just set the DataSource property
MyDataGrid.DataSource = objDataReader;
MyDataGrid.DataBind(); // and bind the control }</script><!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
private void FillDataSet()
{ SqlConnection cn = new SqlConnection();
strConn = "Provider=msdaora;Data Source=robin;User Id=cqsiyo;Password=robinsoft;";
strSql = "Select * from tree";
cn.ConnectionString = strConn;
cn.Open();
SqlDataAdapter cmd = new SqlDataAdapter(strSql,cn);
DataSet ds = new DataSet();
cmd.Fill(ds);
cn.Close();
}
cmd=new OleDbCommand(strSQL,cn);
OleDbDataReader dr=cmd.ExecuteReader();
DataSet ds=new DataSet();
==================
变量cmd 是从没有被定义的,所以有错!!!!!!
应为:
OleDbCommand cmd=new OleDbCommand(strSQL,cn);
OleDbDataReader dr=cmd.ExecuteReader();
DataSet ds=new DataSet();
你能不能用try catch 扑捉一下你链接串是否有错误!?try
{
OleDbConnection cn=new OleDbConnection();
strConn="Provider=msdaora;Data Source=robin;User Id=cqsiyo;Password=robinsoft;";
cn.ConnectionString=strConn;
cn.open();
}
catch
{}
{ OleDbConnection conn = new OleDbConnection();
strConn = "Provider=msdaora;Data Source=robin;User Id=cqsiyo;Password=robinsoft;";
strSql = "Select * from tree";
conn.ConnectionString = strConn;
conn.Open();
OleDbDataAdapter adp = new OleDbDataAdapter(strSql,conn);
DataSet ds = new DataSet();
adp.Fill(ds,"tree");
conn.Close();
}