连接数据库

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

namespace DALayer {    public class BeasClass     {         public string strConn = string.Empty;         public SqlConnection mySqlConnection;            /// <summary>         /// 连接数据库 公共         /// </summary>         public BeasClass()       {           AppSettingsReader configurationAppSettings = new AppSettingsReader();           strConn = ((string)(configurationAppSettings.GetValue("ConnectionString",               typeof(string))));           this.mySqlConnection = new SqlConnection(strConn);       }         /// <summary>         /// 执行sql语句的借用         /// </summary>         /// <returns></returns>         public int meassql(string strsql)         {             int ruselt = 0;             try             {                 mySqlConnection.Open();                 SqlCommand mySqlCommand = new SqlCommand();                 mySqlCommand.CommandText = strsql;                 mySqlCommand.Connection = mySqlConnection;                 ruselt = mySqlCommand.ExecuteNonQuery();             }             catch             {                 throw;             }             finally             {                 mySqlConnection.Close();             }             return ruselt;         }         /// <summary>         /// 获取表所要借用的方法         /// </summary>         /// <param name="strsql"></param>         /// <returns></returns>         public DataTable meastable(string strsql)         {             DataSet myDataSet = new DataSet();             try             {                 mySqlConnection.Open();                 SqlCommand mySqlCommand = new SqlCommand();                 mySqlCommand.CommandText = strsql;                 mySqlCommand.Connection = mySqlConnection;                 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySqlCommand);                 mySqlDataAdapter.Fill(myDataSet);             }             catch             {                 throw;             }             finally             {                 mySqlConnection.Close();             }             return myDataSet.Tables[0];         }         /// <summary>         ///  获取数据集所要的方法         /// </summary>         /// <param name="strsql"></param>         /// <returns></returns>         public DataSet measDataset(string strsql)         {             DataSet myDataSet = new DataSet();             try             {                 mySqlConnection.Open();                 SqlCommand mySqlCommand = new SqlCommand();                 mySqlCommand.CommandText = strsql;                 mySqlCommand.Connection = mySqlConnection;                 SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySqlCommand);                 mySqlDataAdapter.Fill(myDataSet);             }             catch             {                 throw;             }             finally             {                 mySqlConnection.Close();             }             return myDataSet;         }         /// <summary>         /// 获取数据的第一行第一列         /// </summary>         /// <param name="strsql"></param>         /// <returns></returns>         public object measRows(string strsql)         {             object ruselt = null;             try             {                 mySqlConnection.Open();                 SqlCommand mySqlCommand = new SqlCommand();                 mySqlCommand.CommandText = strsql;                 mySqlCommand.Connection = mySqlConnection;                 ruselt = mySqlCommand.ExecuteScalar();             }             catch             {                 throw;             }             finally             {                 mySqlConnection.Close();             }             return ruselt;         }         #region  public int StoreProExcNonQuery(string paramStorePrmName, params SqlParameter[] paramCmdParms) 存储过程,(insert、update、delete) 返回影响行数         /// <summary>         /// 存储过程,(insert、update、delete) 返回影响行数         /// </summary>         /// <param name="paramStorePrmName">存储过程名称</param>         /// <param name="paramCmdParms">存储过程参数</param>         /// <returns>影响行数</returns>         public int StoreProExcNonQuery(string paramStorePrmName, params SqlParameter[] paramCmdParms)         {             int result = 0;             try             {                 this.mySqlConnection.Open();                 SqlCommand myCommand = new SqlCommand();                 myCommand.Connection = mySqlConnection;                 myCommand.CommandText = paramStorePrmName;                 myCommand.CommandType = CommandType.StoredProcedure;                 // 初始化参数                 foreach (SqlParameter parm in paramCmdParms)                 {                     myCommand.Parameters.Add(parm);                 }                 result = myCommand.ExecuteNonQuery();                 myCommand.Parameters.Clear();             }             catch             {                 ;             }             finally             {                 mySqlConnection.Close();             }             return result;         }         #endregion

    } }

App.config: <configuration>  <appSettings>   <add key="ConnectionString" value="server =.;database=Ligon;uid=sa;pwd=*******;" />  </appSettings> </configuration>

连接数据库,布布扣,bubuko.com

连接数据库

上一篇:【pandas】求两列时间差unsupported operand type(s) for -: 'method' and 'method'


下一篇:sql产生随机数