帮忙啦!有关C#中数据库连接的问题!!!!

Explorerwen 2003-06-29 09:13:39
我装上.net后,在工具箱中的数据组建面板中只有Dataset组建、没有DataReader组建,请问用SQLConnection和SQLCommand怎样分别和Dataset,DataReader连接数据库?在和DataGrid表连接起来!
谢谢各位了!
...全文
38 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovered 2003-07-01
  • 打赏
  • 举报
回复
[v]
declude 2003-07-01
  • 打赏
  • 举报
回复
webform下有;
winform下没有,但是有DataBindings 属性和 SetDataBinding 方法
Explorerwen 2003-07-01
  • 打赏
  • 举报
回复
请问一下DataGrid控件有DataBind()方法吗?
我编译时报错没有DataBind()的方法。
多谢各位了!
tianwo2 2003-06-29
  • 打赏
  • 举报
回复
1。
//连接数据库
string str = "user id=sa;password=;initial catalog=test;data source=MZW;Connect Timeout=30";
SqlConnection conn = new SqlConnection(str);
//付给dataset控件
string select = "select * from xsxx";
DataSet ds = new DataSet();
SqlDataAdapter cmm = new SqlDataAdapter(select,conn);
cmm.Fill(ds,"xsxx");
//绑定DataGrid
DataGrid1.DataSource = ds.Tables["xsxx"].DefaultView;
DataGrid1.DataBind();

2。
//连接数据库
string str = "user id=sa;password=;initial catalog=test;data source=MZW;Connect Timeout=30";
SqlConnection conn = new SqlConnection(str);
//执行command
string select = "select * from xsxx";
SqlCommand cmd= new SqlCommand(select,conn);
cmd.Connection.Open();
//付给SqlDataReader
SqlDataReader myReader;
myReader = cmd.ExecuteReader();
3。
//连接数据库方法
//在web.config文件中加入下列代码
<appSettings>
<add key="contodata" value="user id=sa;password=;initial catalog=test;data source=MZW;Connect Timeout=30"/>
</appSettings>
//连接数据库,并且使用名字空间 using System.Configuration;
string strconn = ConfigurationSettings.AppSettings["contodata"];
SqlConnection conn = new SqlConnection(strconn);

zhangzs8896 2003-06-29
  • 打赏
  • 举报
回复
p_staff.cs下:
public DataSet staff(string xueli)
{

SqlDataAdapter MyAdapter= new SqlDataAdapter();

// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["test"]); //test是web.config下设置的连接串
MyAdapter.SelectCommand = new SqlCommand("p_staff", myConnection);

// Mark the Command as a SPROC
MyAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;

SqlParameter parameterxueli = new SqlParameter("@xueli", SqlDbType.VarChar, 6);
parameterxueli.Value =xueli;
MyAdapter.SelectCommand.Parameters.Add(parameterxueli);
// Open the database connection and execute the command
DataSet ds=new DataSet();
myConnection.Open();
try
{
MyAdapter.Fill(ds);
return ds;
}
finally
{
myConnection.Close();
}
}//end
aspx下按钮click事件:
private void Btn_check_Click(object sender, System.EventArgs e)
{
DataGrid1.CurrentPageIndex=0;
LOVECsharp.p_staff myErp=new LOVECsharp.p_staff(); // LOVECsharp为项目名称
DataSet ds=myErp.staff(Ddl1.SelectedItem.Text.ToString());
DataView source = ds.Tables[0].DefaultView;
source.Sort=sortfield;
DataGrid1.DataSource=source;
DataGrid1.DataBind();
}
zhangzs8896 2003-06-29
  • 打赏
  • 举报
回复
sorry,少了SqlConnection.看下面:
SqlConnection con=new SqlConnection("server=zhangzs;database=test;uid=sa;pwd=");
string str="select * from Table ";
con.Open();
SqlCommand cmd =new SqlCommand(str,con);
DataGrid1.DataSource=cmd.ExecuteReader();
DataGrid1.DataBind();
con.Close();
zhangzs8896 2003-06-29
  • 打赏
  • 举报
回复
string str="select * from Table ";
con.Open();
SqlCommand cmd =new SqlCommand(str,con);
DataGrid1.DataSource=cmd.ExecuteReader();
DataGrid1.DataBind();
con.Close();

110,557

社区成员

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

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

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