1、配置文件APP.confightml
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="sqlconn" value="Data Source=.; DataBase=db_SAIC; User ID=sa; Password=WIPsys8818;Connect Timeout=2500" /> </appSettings> </configuration>
2、DataSource 類sql
添加引用 System.Configuration數據庫
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; using System.Data.SqlClient; using System.Data; namespace Saic.Class { class DataSource { //訪問配置文件 public static string connString = ConfigurationManager.AppSettings["sqlconn"]; public static SqlConnection connection = new SqlConnection(connString); //數據庫操做,返回記錄數 public int ExecuteSqlGetCount(string strSql) { using (SqlCommand cmd = new SqlCommand(strSql, connection)) { try { if (connection.State == ConnectionState.Closed) { connection.Open(); } int rows = (int)cmd.ExecuteScalar(); return rows; } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } finally { connection.Close(); } } } //返回數據集 public DataTable ExecuteSqlGetUser(string strSql) { using (SqlCommand cmd = new SqlCommand(strSql, connection)) { try { if (connection.State == ConnectionState.Closed) { connection.Open(); } DataTable dt = new DataTable(); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; sda.Fill(dt); connection.Close(); return dt; } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } finally { connection.Close(); } } } //用於返回單行結果 public string ExecuteOne(string strSql) { using (SqlCommand cmd = new SqlCommand(strSql, connection)) { try { if (connection.State == ConnectionState.Closed) { connection.Open(); } //ExecuteScalar() 執行查詢方法,返回單個值 string result; if (cmd.ExecuteScalar() != null) { result = cmd.ExecuteScalar().ToString(); } else { result = null; } connection.Close(); return result; } catch (SqlException ex) { throw new Exception(ex.Message); } finally { connection.Close(); } } } //增刪 public int ExecuteSqlUpdate(string strSql) { using (SqlCommand cmd = new SqlCommand(strSql, connection)) { try { if (connection.State == ConnectionState.Closed) { connection.Open(); } int rows = cmd.ExecuteNonQuery(); connection.Close(); return rows; } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } finally { connection.Close(); } } } //調用SQL存儲過程 Proc_BS public int ExecuteProcGetParm(string strProc, int scaleC, int scaleAB, int scaleC_1, int scaleAB_1) { using (SqlCommand cmd = new SqlCommand(strProc, connection)) { try { cmd.CommandTimeout = 0; cmd.CommandText = strProc;// "Proc_BS"; cmd.CommandType = CommandType.StoredProcedure; SqlParameter parm = cmd.Parameters.Add("@scaleC", SqlDbType.Int); parm.Value = scaleC; parm = cmd.Parameters.Add("@scaleAB", SqlDbType.Int); parm.Value = scaleAB; parm = cmd.Parameters.Add("@scaleC_1", SqlDbType.Int); parm.Value = scaleC_1; parm = cmd.Parameters.Add("@scaleAB_1", SqlDbType.Int); parm.Value = scaleAB_1; parm = cmd.Parameters.Add("@rowcount", SqlDbType.Int); parm.Direction = ParameterDirection.Output; if (connection.State == ConnectionState.Closed) { connection.Open(); SqlDataReader dr = cmd.ExecuteReader(); int rows = int.Parse(cmd.Parameters["@rowcount"].Value.ToString()); //cmd.ExecuteNonQuery(); connection.Close(); return rows; } return 0; } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } finally { connection.Close(); } } } } }