标签:
基本和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; } } } }
标签:
原文地址:http://www.cnblogs.com/lilixiang-go/p/5044277.html