码迷,mamicode.com
首页 > 数据库 > 详细

Winform下的数据库操作类

时间:2015-12-14 10:50:53      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

基本和web区别不大,就是连接字符串改改。

web的配置文件叫web.config。而Winform的则是app.config

  <appSettings>
    <add key="connectionstring" value="server=192.168.0.119;uid=sa;pwd=sa;database=ZhanBan"/>
  </appSettings>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace DBUtility
{
    public class SqlHelper
    {
        public SqlHelper() 
        { 
        }
        private static string ConnectString = System.Configuration.ConfigurationManager.AppSettings["connectionstring"];
        public void ExecuteNonQuery(string sql)//执行sql语句    
        {
            using (SqlConnection myconn = new SqlConnection(ConnectString))
            {
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(sql, myconn);
                mycmd.ExecuteNonQuery();
            }
        }
        public int ExecutePro(string proname, SqlParameter[] parameters)
        {
          using(SqlConnection myconn = new SqlConnection(ConnectString)) 
          {  
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(proname, myconn);
                mycmd.CommandType = CommandType.StoredProcedure;
                mycmd.CommandText = proname;
            if (parameters != null)
            {
                foreach (SqlParameter parameter in parameters)
                {
                    mycmd.Parameters.Add(parameter);
                }
            }
                return mycmd.ExecuteNonQuery();
          }
        }
        public DataTable ExecuteGetDT(string sql) 
        {
            using(SqlConnection myconn = new SqlConnection(ConnectString))
            {
                myconn.Open();
                SqlCommand mycmd = new SqlCommand(sql, myconn);
                SqlDataAdapter mydata_ad = new SqlDataAdapter(mycmd);
                DataSet ds = new DataSet();
                DataTable dt = new DataTable("table");
                ds.Tables.Add(dt);
                mydata_ad.Fill(ds, "table");
                return dt;
            }
        }

    }

}

 

Winform下的数据库操作类

标签:

原文地址:http://www.cnblogs.com/lilixiang-go/p/5044277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!