欢迎来到天天文库
浏览记录
ID:18457490
大小:47.50 KB
页数:6页
时间:2018-09-18
《c#连接sql数据库的使用方法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、c#连接SQL数据库的使用方法最佳答案1.提取单条记录//usingSystem.Data;//usingSystem.Data.SqlClient;using(SqlConnectioncn=newSqlConnection(%%1))//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString//DataSource=myServerAddress;InitialCatalog=myDataBase;UserId=myUsername;Password=myPassword;{SqlComm
2、andcmd=newSqlCommand("SelectCount(*)Fromjobs",cn);cn.Open();%%2=cmd.ExecuteScalar();//Message.InnerHtml}2.单值比较//usingSystem.Data;//usingSystem.Data.SqlClient;using(SqlConnectioncn=newSqlConnection(%%1))//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString//DataSource=mySer
3、verAddress;InitialCatalog=myDataBase;UserId=myUsername;Password=myPassword;{SqlCommandcmd=newSqlCommand("SelectCount(*)Fromjobs",cn);cn.Open();%%2=cmd.ExecuteScalar();//Message.InnerHtmlif(%%2==%%3){%%4}}3.显示表格//usingSystem.Data;//usingSystem.Data.SqlClient;DataSetds=null;using(SqlConnectioncn=newSq
4、lConnection(%%1))//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString//DataSource=myServerAddress;InitialCatalog=myDataBase;UserId=myUsername;Password=myPassword;{cn.Open();cmd=newSqlDataAdapter("Select*FromAuthor",cn);ds=newDataSet();cmd.Fill(ds,%%2);//"作者"MyDataGrid.Dat
5、aSource=ds.Tables(%%2).DefaultView;//"作者"MyDataGrid.DataBind();}4.操作表格//usingSystem.Data;//usingSystem.Data.SqlClient;using(SqlConnectioncn=newSqlConnection(%%1))//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString//DataSource=myServerAddress;InitialCatalog=myDataBase;Use
6、rId=myUsername;Password=myPassword;{cn.Open();cmd=newSqlDataAdapter("Select*FromAuthor",cn);ds=newDataSet();cmd.Fill(ds,%%2);//"作者"MyDataGrid.DataSource=ds.Tables(%%2).DefaultView;//"作者"MyDataGrid.DataBind();}5.数值范围查询//usingSystem.Data;//usingSystem.Data.SqlClient;using(SqlConnectioncn=newSqlConnect
7、ion(%%1))//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString//DataSource=myServerAddress;InitialCatalog=myDataBase;UserId=myUsername;Password=myPassword;{}6.关闭时断开连接//using
此文档下载收益归作者所有