您的位置:首页 > 数据库

c# 链接数据库的方法

2009-04-09 20:59 239 查看
Access
string mdbmap = Server.MapPath("book.mdb");
string mbookid= Request.QueryString["bookid"];
OleDbConnection myConn = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" + mdbmap);
myConn.Open();
OleDbDataAdapter thisAdapter= new OleDbDataAdapter("SELECT * FROM BookIndex ",myConn);
OleDbCommandBuilder thisBuilder=new OleDbCommandBuilder(thisAdapter);
DataSet thisDataSet=new DataSet();
thisAdapter.Fill(thisDataSet,"BookIndex");
int nTotals=thisDataSet.Tables["BookIndex"].Rows.Count;

for(int i=0;i<nTotals;i++)
{
myBI = new BookIndex();
myBI.id =(int)thisDataSet.Tables["BookIndex"].Rows[i]["id"];
myBI.bookcode =(string) thisDataSet.Tables["BookIndex"].Rows[i]["bookcode"];
myBI.bookintro =(string) thisDataSet.Tables["BookIndex"].Rows[i]["bookintro"];
myBI.booktitle =(string) thisDataSet.Tables["BookIndex"].Rows[i]["booktitle"];
myBIs.Add(myBI);

}

myConn.Close();

Sql server
首先在web.config中加入

<appSettings>
<add key="SqlStr" value="workstation id=aa;server=192.168.0.200;packet size=4096;user id=sa;initial catalog=smokeless;persist security info=True;password=111"></add>
</appSettings>

以下是代码

System.Data.SqlClient.SqlConnection sqlConnection2;
System.Data.SqlClient.SqlCommand sqlCommand2;
string customSetting = System.Configuration.ConfigurationSettings.AppSettings["SqlStr1"];
customSetting = customSetting + ";initial catalog=" + m_dbname;
sqlConnection2 = new SqlConnection();
sqlConnection2.ConnectionString = customSetting ;
sqlConnection2.Open();

str = "INSERT INTO [MTQueue] (mobile,fee_type,fee_code,msg,datetime,srcmobile,serviceID,msgfmt,chargenumber,linkID) VALUES ('" ;
str =str + m_srcmobile + "'," ;
str =str + "1" + ",'" ;
str =str + "" + "','" ;
str =str + "您已经定购了的产品,如果需要退定请发送TDJY到04440(本条信息免费)" + "','" ;
str =str + m_datetime + "','" ;
str =str + m_srcmobile + "','" ;
str =str + "JY" + "'," ;
str =str + "15" + ",'" ;
str =str + "m_srcmobile" + "','" ;
str =str + m_linkID + "')" ;

sqlCommand2 = new SqlCommand(str,sqlConnection2) ;
sqlCommand2.ExecuteNonQuery();

sqlConnection2.Close() ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: