C#明mySQL连接语句_C#连接各种数据库语句(实例)

//Access

using System.Data;

using System.Data.OleDb;

connString ="Provider =Microsoft.Jet.OleDb.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("~/App_Data/MyDataBase.mdb");

//SQL server

using System.Data;

using System.Data.SqlClient;

//SQL2005

connString="Data Source=zhou\\sql2005;Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=123456");

//SQL2008

connString = @"server=LENOVO-AA\MYDATABASE;database=Database;Trusted_Connection=True";

//MySQL

using MySQLDriverCS;

using System.Net;

using System.Text;

using CoreLab.MySql;

using System.Data.Odbc;

using MySql.Data.MySqlClient;

MySQLConnection DBConn = new MySQLConnection(new MySQLConnectionString("localhost","mysql","root","",3306).AsString);

connString = "User Id=root;Host=localhost;Database=qing;password=qing";

//SyBase

Provider=Sybase.ASEOLEDBProvider.2;Initial Catalog=数据库名;User ID=用户名;Data Source=数据源;Extended Properties="";Server Name=ip地址;Network Protocol=Winsock;Server Port Address=5000;

//Oracle

using System.Data;

using System.Data.OracleClient;

connString="Data Source=sky;user=system;password=manager;";

//IBM DB2

connStri = "Driver={IBM DB2 ODBC DRIVER};Database=sample;hostname=192.168.1.46;port=50000;protocol=TCPIP; uid=admin; pwd=admin";

private SqlConnection con; //数据库连接、关闭、释放资源 #region 数据库连接、关闭、释放资源 private void Open() { if (con == null) { //根据Web.Config文件中数据库连接串,建立数据连接 con = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString ()); } try { //判断连接对象状态,如果是关闭,将其打开 if (con.State == System.Data.ConnectionState.Closed) con.Open(); } catch (System.Data.SqlClient.SqlException E) { //如果出现错误,关闭数据连接,并抛出错误信息 this.Close(); throw new Exception(E.Message); } } public void Close() { if (con != null) { con.Close();//关闭连接 } } public void Dispose() { if (con != null) { con.Dispose();//释放连接资源 con = null; } } #endregion #region 参数处理 public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value) //参数处理 { SqlParameter param; if (Size > 0) { param = new SqlParameter(ParamName, DbType, Size); } else { param = new SqlParameter(ParamName, DbType); } param.Direction = ParameterDirection.Input; if (Value != null) { param.Value = Value; } return param; } #endregion #region 将参数添加到SqlCommand当中 private SqlCommand CreateCommand(string procName, SqlParameter[] prams) //将参数添加到SqlCommand当中 { this.Open();//确认打开连接 SqlCommand cmd = new SqlCommand(procName, con); cmd.CommandType = CommandType.Text; //执行类型是命令文本。上面的参数procName即为SQL语句 //cmd.CommandType = CommandType.StoredProcedure; //执行类型是存储过程。上面的参数procName即为存储过程名称 //下面依次把参数传入命令文本 if (prams != null) { foreach (SqlParameter parameter in prams) cmd.Parameters.Add(parameter); } return cmd; } #endregion #region 将参数添加到SqlDataAdapter当中 private SqlDataAdapter CreateDataAdapter(string procName, SqlParameter[] prams) //将参数添加到SqlDataAdapter当中 { this.Open(); SqlDataAdapter dap = new SqlDataAdapter(procName, con); dap.SelectCommand.CommandType = CommandType.Text;//执行类型:命令文本 //dap.SelectCommand.CommandType = CommandType.StoredProcedure;//执行类型:存储过程 if (prams != null) { foreach (SqlParameter parameter in prams) { dap.SelectCommand.Parameters.Add(parameter); } } return dap; } #endregion #region 针对查询(带参) public DataSet RunProcReturn(string procName, SqlParameter[] prams, string tbName) //针对查询(带参) { SqlDataAdapter dap = CreateDataAdapter(procName, prams); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close(); //得到执行成功返回值 return ds; } #endregion #region 针对查询(不带参) public DataSet RunProcReturn(string procName, string tbName) //针对查询(不带参) { SqlDataAdapter dap = CreateDataAdapter(procName, null); DataSet ds = new DataSet(); dap.Fill(ds, tbName); this.Close(); //得到执行成功返回值 return ds; } #endregion #region 针对增删改 public int RunProc(string procName, SqlParameter[] prams) //针对增删改 { SqlCommand cmd = CreateCommand(procName, prams); int i = cmd.ExecuteNonQuery(); this.Close(); return i; } #endregion }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值