資源描述:
《c#連接sql數(shù)據(jù)庫的使用方法》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、c#連接SQL數(shù)據(jù)庫的使用方法最佳答案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.數(shù)值范圍查詢//usingSystem.Data;//usingSystem.Data.SqlClient;using(SqlConnectioncn=newSqlConnect
7、ion(%%1))//ConfigurationManager.ConnectionStrings["db2ConnectionString"].ConnectionString//DataSource=myServerAddress;InitialCatalog=myDataBase;UserId=myUsername;Password=myPassword;{}6.關(guān)閉時斷開連接//using