如何在.NET中访问MySQL数据库

如何在.NET中访问MySQL数据库,第1张

1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序

mysql-connector-net-6.3.8.msi

(2)安装mysql-connector-net

然后直接在Windows *** 作系统安装 mysql-connector-net-6.3.8.msi

(3)封装数据库访问组件DbConnectionMySQL

/// <summary>

/// MySQL数据库

/// 版本 mysql-connector-net-6.3.8.msi

/// </summary>

[Serializable]

public class DbConnectionMySQL : DbConnectionWrapper

{

public DbConnectionMySQL(string pConnectionString)

: base(pConnectionString)

{

this.m_dbconn = new MySqlConnection(pConnectionString)

this.m_DbConnState = DbConnState.Free

}

//--

public override DbDataAdapter GetDbDataAdapter()

{

return new MySqlDataAdapter()

}

public override DbDataAdapter GetDbDataAdapter(DbCommand dbCommand)

{

return new MySqlDataAdapter(dbCommand as MySqlCommand)

}

public override DbCommand GetDbCommand()

{

return new MySqlCommand()

}

public override DbConnection GetDbConnection()

{

return new MySqlConnection()

}

public override DbCommandBuilder GetDbCommandBuilder()

{

return new MySqlCommandBuilder()

}

public override DataProviderType GetCurrentDataProviderType()

{

return DataProviderType.Sql

}

public override bool IsExistsTable(string TableName, string UserName)

{

#region information

bool rbc = false //TABLES表中去查询 table_name

string dSql = "select * from TABLES where table_name='" + TableName + "'"

DataSet ds = this.ExecuteDataSet(dSql)

if (ds != null)

{

if (ds.Tables[0].Rows.Count >0)

{

rbc = true

}

else

{

rbc = false

}

}

else

{

rbc = false

}

return rbc

#endregion

}

public override bool IsExistsField(string FieldName, string TableName)

{

#region information

bool rbc = false

string dSql = ""

dSql = "select * from " + TableName + " where 1<>1"

DataSet ds = this.ExecuteDataSet(dSql)

if (ds != null)

{

DataTable dt = ds.Tables[0]

for (int j = 0j <dt.Columns.Countj++)

{

if (dt.Columns[j].ColumnName.ToString().ToUpper() == FieldName.ToString().ToUpper())

{

rbc = true

goto Return_End

}

}

dt.Dispose()

dt = null

}

ds.Dispose()

ds = null

Return_End:

return rbc

#endregion

}

public override char ParameterChar

{

get

{

return ':' //SQLite的参数符号为:

}

}

public override DbParameter CreateParameter(string name, object value)

{

return new MySqlParameter(name, value)

}

public override DbParameter CreateParameter(string name)

{

DbParameter dbp = new MySqlParameter()

dbp.ParameterName = name

return dbp

}

public override DbParameter CreateParameter(string name, DbType dbtype, object value)

{

DbParameter dbp = new MySqlParameter()

dbp.ParameterName = name

dbp.Value = value

dbp.DbType = dbtype

return dbp

}

public override DbParameter CreateParameter(string name, DbType dbtype, int size, object value)

{

DbParameter dbp = new MySqlParameter()

dbp.ParameterName = name

dbp.Value = value

dbp.DbType = dbtype

dbp.Size = size

return dbp

}

}

属性进行封装??

简单说 实际上就是 给 属性 可读可写的 权限就是了;

.NET 中 读属性 用get,写属性 用set 关键字

比如:一下就是对四个属性的简单封装

using System

namespace Common

{

public class stuCommon

{

/// <summary>

/// 把数据库的四个属性封装成实体类stuCommon

/// </summary>

private string _id

public string ID

{

get { return _id} //赋给ID这个属性 对外的 读 属性,(读-->返回值)

set { _id = value}//赋给ID这个属性 对外的 写 属性,(写—>赋值 )

}

private string _name

public string Name

{

get { return _name}

set { _name = value}

}

private int _age

public int Age

{

get { return _age}

set { _age = value}

}

private string _sex

public string Sex

{

get { return _sex}

set { _sex = value}

}

}

}

望采纳~~~

1.web.config 中

<add key="ConnectionString" value="server=(local)uid=sapwd=123456database=News"/>

调用的时候

string strConn = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString()

SqlConnection Conn = new SqlConnection(strConn)

2.或者不用web.config直接在文件中写

SqlConnection conn = new SqlConnection("server=.\\SQLEXPRESSuid=sapwd=123456database=login")

如何是Express版的数据库,一定要在服务器名的后面加上 \\SSQLEXPRESS


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6718540.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存