连接数据库的步骤

CBJ2049011 2003-07-11 09:11:49
请教在DBGRID中显示数据内容
SQL SERVER数据库
都需要什么控件,控件怎么关联,要求执行SQL语句
...全文
51 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Explorerwen 2003-07-14
  • 打赏
  • 举报
回复
你是连接winform 吧,不是web吧!
将DataGrid1.setdatabinding();
或databindings属性
bluex 2003-07-14
  • 打赏
  • 举报
回复
上面说的都对
雪狼1234567 2003-07-14
  • 打赏
  • 举报
回复
使用如下的代码:
使用DataGrid,代码如下:
System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection("server=localhost;database=northWind;uid=sa;password=194910");
conn.Open();
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select * from customers",conn);
dt = new System.Data.DataSet();
da.Fill(dt,"customers");
this.dataGrid1.DataSource = dt;
this.dataGrid1.DataMember = "customers";
或者:
this.dataGrid1.SetDataBinding(dt,"customers");
CBJ2049011 2003-07-14
  • 打赏
  • 举报
回复
zhangzs8896(小二)
你的方法我试过了,不成工主要是
C:\Documents and Settings\liren\My Documents\Visual Studio Projects\waDataBase\Form1.cs(111): “System.Windows.Forms.DataGrid”并不包含对“DataBind”的定义
zhangzs8896 2003-07-11
  • 打赏
  • 举报
回复
也可以是用下面的
SqlConnection con=new SqlConnection("server=机器名;database=数据库;uid=sa;pwd=密码");
string str="select * from yourtable";
con.Open();
SqlCommand cmd =new SqlCommand(str,con);
DataGrid1.DataSource=cmd.ExecuteReader();
DataGrid1.DataBind();
con.Close();
laixiang 2003-07-11
  • 打赏
  • 举报
回复
SqlConnection conn=new SqlConnection("user id=sa;password=9910124;initial catalog=oa;data source=localhost;Connect Timeout=30
");
conn.Open();
string comm="select * from jhtdata";
SqlDataAdapter thisAdapter=new SqlDataAdapter(comm,conn);
DataSet thisSet=new DataSet();
thisAdapter.Fill(thisSet,"jhtdata");
this.dataGrid1.DataSourse=thisSet.Tables["jhtdata"].DefaultView;
conn.Close();
tanrui 2003-07-11
  • 打赏
  • 举报
回复
先用SqlConnection连数据库
把SqlConnection付给SqlCommand
用SqlCommand执行sql语句,得到SqlDataReader
用SqlDataReader的DataBind方法把数据输出到Grid里去

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧