c#数据库操作

用配置文件连接数据库 string connstring = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnectionString"].ConnectionString; myconn = new SqlConnection(connstring); Web.config中添加 数据库操作类 public class DatabaseTool { public DatabaseTool(){ } private SqlConnection myconn = null; private SqlDataAdapter da = null; private DataSet ds = new DataSet(); private string connstring = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["sqlConnectionString"].ConnectionString; private string returnvaluestring = "return"; public string RetrunValuestring { get { return returnvaluestring; } } public SqlConnection GetConnection()//建立连接 { myconn = new SqlConnection(connstring); return myconn; } //执行SQL语句返回Dataset public DataSet SelectDataByDateSet(SqlCommand comm, string tableName) { da = new SqlDataAdapter(comm); ds.Clear(); myconn.Open(); da.Fill(ds, tableName); myconn.Close(); return ds; } //执行SQL语句(增删改) public void ExecuteData(SqlCommand comm) { try { myconn.Open(); comm.ExecuteNonQuery(); } catch (SqlException ex) { throw new Exception(ex.Message, ex); } finally { if (comm.Connection.State == ConnectionState.Open) { comm.Connection.Close(); } } } } 使用例子 DataSet ds = new DataSet(); DatabaseTool dbt = new DatabaseTool(); SqlConnection myconn = dbt.GetConnection(); try { string strSQL = "INSERT INTO greenevent(Title, Description,[Start-time],[End-time],UserID,SceneID,[Join-number],Contribution) VALUES(@title,@description,@starttime,@endtime,@userid,@sceneventid,@joinnum,@contribution)"; SqlCommand mycomm = new SqlCommand(strSQL, myconn); mycomm.Parameters.Add("@title", SqlDbType.VarChar, 50).Value = title; mycomm.Parameters.Add("@description", SqlDbType.VarChar, 50).Value = description; mycomm.Parameters.Add("@starttime", SqlDbType.DateTime).Value = starttime; mycomm.Parameters.Add("@endtime", SqlDbType.DateTime).Value = endtime; mycomm.Parameters.Add("@userid", SqlDbType.Int).Value = userid; mycomm.Parameters.Add("@sceneventid", SqlDbType.Int).Value = sceneventid; mycomm.Parameters.Add("@joinnum", SqlDbType.Int).Value = joinnum; mycomm.Parameters.Add("@contribution", SqlDbType.Int).Value = contribution; dbt.ExecuteData(mycomm); string strSQL2 = "select ID from greenevent where Title=@title2 and Description=@description2 and [Start-time]=@starttime2 and [End-time]=@endtime2 and UserID=@userID2 and Sceneid=@sceneid2 and [Join-number]=@joinnum2 and Contribution=@contribution2"; SqlCommand mycomm2 = new SqlCommand(strSQL2, myconn); mycomm2.Parameters.Add("@title2", SqlDbType.VarChar, 50).Value = title; mycomm2.Parameters.Add("@description2", SqlDbType.VarChar, 50).Value = description; mycomm2.Parameters.Add("@userid2", SqlDbType.Int).Value = userid; mycomm2.Parameters.Add("@starttime2", SqlDbType.DateTime).Value = starttime; mycomm2.Parameters.Add("@endtime2", SqlDbType.DateTime).Value = endtime; mycomm2.Parameters.Add("@sceneid2", SqlDbType.Int).Value = sceneventid; mycomm2.Parameters.Add("@joinnum2", SqlDbType.Int).Value = joinnum; mycomm2.Parameters.Add("@contribution2", SqlDbType.Int).Value = contribution; ds = dbt.SelectDataByDateSet(mycomm2, "[MYTABLE]"); int eventid = Convert.ToInt32(ds.Tables["[MYTABLE]"].Rows[0][0]); GreenTagService gs = new GreenTagService(); int[] tagids = new int[tagnames.Length]; for (int i = 0; i < tagnames.Length; i++) { tagids[i] = gs.AddGreenTag(tagnames[i]); string strsql = "insert into event2tag(Bid,Tid,Cout) values (@eventid,@tagid,1)"; SqlCommand mycommi = new SqlCommand(strsql, myconn); mycommi.Parameters.Add("@eventid", SqlDbType.Int).Value = eventid; mycommi.Parameters.Add("@tagid", SqlDbType.Int).Value = tagids[i]; dbt.ExecuteData(mycommi); } for (int i = 0; i < tagnames.Length - 1; i++) { for (int j = tagnames.Length - 1; j > 0 & j > i; j--) { string strsqlj = "select ID from greentag2greentag where TID1=@TD1 and TID2=@TD2"; SqlCommand mycommj = new SqlCommand(strsqlj, myconn); mycommj.Parameters.Add("@TD1", SqlDbType.Int).Value = tagids[i]; mycommj.Parameters.Add("@TD2", SqlDbType.Int).Value = tagids[j]; ds = dbt.SelectDataByDateSet(mycommj, "[MYTABLE]"); if (ds.Tables["[MYTABLE]"].Rows.Count != 0)//存在 { string strsqlj2 = "update greentag2greentag set Count=Count+1 where TID1=@TD12 update greentag2greentag set Count=Count+1 where TID2=@TD12"; SqlCommand mycommj2 = new SqlCommand(strsqlj2, myconn); mycommj2.Parameters.Add("@TD12", SqlDbType.Int).Value = tagids[i]; dbt.ExecuteData(mycommj2); } else//不存在 { string strsqlj3 = "insert into greentag2greentag(TID1,TID2,Count) values (@TD13,@TD23,1) insert into greentag2greentag(TID1,TID2,Count) values (@TD23,@TD13,1)"; SqlCommand mycommj3 = new SqlCommand(strsqlj3, myconn); mycommj3.Parameters.Add("@TD13", SqlDbType.Int).Value = tagids[i]; mycommj3.Parameters.Add("@TD23", SqlDbType.Int).Value = tagids[j]; dbt.ExecuteData(mycommj3); } } } return eventid; } catch { return -1; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值