c# 链接数据库的方法

 

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() ;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值