初级问题,求几种数据连接并绑定GirdView 的范例程序 在网上看了有许多的做法,写法也不一样。有的有OleDbConnection 连接,有的又用SqlConnection,连接后数据绑定也有的用OleDb.OleDbDataAdapter,有的又用别的东东,都搞不清楚,后面在绑定GirdView 时的代码该如何写。哪位热心的大侠给用几种方法做一个范例程序,简单说明一下其优销点. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 OleDbConnection 连接和SqlConnection连接是针对不同的数据库说的,先问下楼主,你自己写过简单的程序么?自学的? SqlConnection conn =null; SqlDataAdapter dapt = null; DataSet ds = new DataSet(); string strConnectionString = "Data Source=.;initial catalog=数据库;uid=sa;pwd=xxx"; string strSQL = "SELECT 字段 from 表" try { conn = new SqlConnection(strConnectionString); conn.Open(); dapt = new SqlDataAdapter(strSQL,conn); dapt.Fill(ds,"table1"); DataTable dt = ds.Tabls[0]; this.GirdView1.DataSource= dt.DefaultView; this.GridView1.DataBind(); } catch(System.Exception e) { this.Text1.Text = "错误"+e.Message; } finally { if(conn!=null) conn.Dispose(); if(dapt!=null) dapt.Dispose(); } 帮你顶。。baidu吧。。N多例子 mssql 用SqlConnection(SqlClient微软专门为mssql提供的)Oracle 用OracleConnection(OracleClient微软专门为oracle提供的)其他用oledb,odbc性能可能要差些.无论用什么都要配对使用.如用SqlConnection,SqlCommand,SqlDataAdapter,等等OracleConnection,OracleCommand,OracleDataAdapter等等OleDbConnection ,OleDbCommand,等.都要配对.不能牛头对马嘴.不管你用的什么形式连接数据.对GridView的帮定没有关系.不管什么方式的连接.你要么返回的DataSet,要么DataTable.DataSet,DataTable没有区别.帮定GridViewGridViewID.DataSourse=DataSetID,or DataTableIDGridViewID.DataBing(); to yicel(无心止痛),就是没太明白SqlDataAdapter 这是个是什么鸟东东,有SqlCommand和dataset ,它还起什么作用? to 慕白,为何我不要 DataTable dt = ds.Tabls[0];这句,而直接 GirdView1.DataSource= ds;也同样的能把数据绑定到gridview 中这两种做法有什么区别,为何要用DataTable dt = ds.Tabls[0]; this.GirdView1.DataSource= dt.DefaultView 来做一次转换。 它默认就是每一张表.其实一样不过用dataview比datable效率要高 我就又有一点搞不明白了。 dapt = new SqlDataAdapter(strSQL,conn); dapt.Fill(ds,"table1");这两句中(strSQL)不就是已限制只能是打开一个table 吗?为何会默认就是每一张表,难道还能打开多张表不成?谢谢! 可以的.如strSQL = "select 1;select 2;"这样ds中就有两张表. SqlDataReader reader = dataCommand.ExecuteReader();这句代码中的dataCommand 又是什么东东,该如保理解。 Ajax开发模式问题 jquery 小问题 iframe自适应高度 .net过滤非法字符 data_list.aspx.cs是不是后台有相对应的文件的? 如何使用template? 一个dropdownlist的数据邦定问题,有经验的高手过来帮帮我吧? WebApplication项目不能打开的问题 问题简单,分数高,快快行动。 请问怎么远程管理sql server 2000数据库? String.Format格式化问题 ?? 有实现这样功能的sql语句么???
先问下楼主,你自己写过简单的程序么?自学的?
SqlDataAdapter dapt = null;
DataSet ds = new DataSet();
string strConnectionString = "Data Source=.;initial catalog=数据库;uid=sa;pwd=xxx";
string strSQL = "SELECT 字段 from 表"
try
{
conn = new SqlConnection(strConnectionString);
conn.Open();
dapt = new SqlDataAdapter(strSQL,conn);
dapt.Fill(ds,"table1");
DataTable dt = ds.Tabls[0];
this.GirdView1.DataSource= dt.DefaultView;
this.GridView1.DataBind();
}
catch(System.Exception e)
{
this.Text1.Text = "错误"+e.Message;
}
finally
{
if(conn!=null)
conn.Dispose();
if(dapt!=null)
dapt.Dispose();
}
Oracle 用OracleConnection(OracleClient微软专门为oracle提供的)
其他用oledb,odbc性能可能要差些.无论用什么都要配对使用.
如用SqlConnection,SqlCommand,SqlDataAdapter,等等
OracleConnection,OracleCommand,OracleDataAdapter等等
OleDbConnection ,OleDbCommand,等.都要配对.不能牛头对马嘴.不管你用的什么形式连接数据.
对GridView的帮定没有关系.不管什么方式的连接.你要么返回的DataSet,要么DataTable.
DataSet,DataTable没有区别.
帮定GridView
GridViewID.DataSourse=DataSetID,or DataTableID
GridViewID.DataBing();
GirdView1.DataSource= ds;也同样的能把数据绑定到gridview 中这两种做法有什么区别,为何要用DataTable dt = ds.Tabls[0];
this.GirdView1.DataSource= dt.DefaultView 来做一次转换。
不过用dataview比datable效率要高
dapt = new SqlDataAdapter(strSQL,conn);
dapt.Fill(ds,"table1");
这两句中(strSQL)不就是已限制只能是打开一个table 吗?为何会默认就是每一张表,难道还能打开多张表不成?
谢谢!
这样ds中就有两张表.
这句代码中的dataCommand 又是什么东东,该如保理解。