如何在.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

}

}

连接MYSQL数据库的方法及示例

方法一:

使用MYSQL推出的MySQL

Connector/Net

is

an

ADO.NET

driver

for

MySQL

该组件为MYSQL为ADO.NET访问MYSQL数据库设计的.NET访问组件。

安装完成该组件后,引用命名空间MySql.Data.MySqlClient

使用命令行编译时:csc

/r:MySql.Data.dll

test.cs

方法二:

通过ODBC访问MYSQL数据库

访问前要先下载两个组件:odbc.net和MYSQL的ODBC驱动(MySQL

Connector/ODBC

(MyODBC)

driver)目前为3.51版

安装完成后,即可通过ODBC访问MYSQL数据库

方法三:

使用CoreLab推出的MYSQL访问组件,面向.NET

安装完成后,引用命名空间:CoreLab.MySql

使用命令编译时:csc

/r:CoreLab.MySql.dll

test.cs

以下为访问MYSQL数据库实例

编译指令:csc

/r:CoreLab.MySql.dll

/r:MySql.Data.dll

test.cs

using

System

using

System.Net

using

System.Text

using

CoreLab.MySql

using

System.Data.Odbc

using

MySql.Data.MySqlClient

class

ConnectMySql

{

public

void

Connect_CoreLab()

{

string

c

MySqlConnection

mycn

=

new

MySqlConnection(constr)

mycn.Open()

MySqlCommand

mycm

=

new

MySqlCommand("select

*

from

shop",mycn)

MySqlDataReader

msdr

=

mycm.ExecuteReader()

while(msdr.Read())

{

if

(msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0))

}

}

msdr.Close()

mycn.Close()

}

public

void

Connect_Odbc()

{

//string

MyC

string

MyC

+

"SERVER=localhost"

+

"DATABASE=test"

+

"UID=root"

+

"PASSWORD=qing"

+

"OPTION=3"

OdbcConnection

MyConn

=

new

OdbcConnection(MyConString)

MyConn.Open()

OdbcCommand

mycm

=

new

OdbcCommand("select

*

from

hello",MyConn)

OdbcDataReader

msdr

=

mycm.ExecuteReader()

while(msdr.Read())

{

if

(msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0))

}

}

msdr.Close()

MyConn.Close()

}

public

void

Connect_Net()

{

string

myC

MySqlConnection

mycn

=

new

MySqlConnection(myConnectionString)

mycn.Open()

MySqlCommand

mycm

=

new

MySqlCommand("select

*

from

hello",mycn)

MySqlDataReader

msdr

=

mycm.ExecuteReader()

while(msdr.Read())

{

if

(msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0))

}

}

msdr.Close()

mycn.Close()

}

public

static

void

Main()

{

ConnectMySql

ms

=

new

ConnectMySql()

ms.Connect_CoreLab()

ms.Connect_Odbc()

Connect_Net()

}

}

repeater:控件内的内容,会根据数据源而循环显示。也就是说,repeater只是一个循环。你可以这样理解。

你这个排版样式。一般都是用li来做。下面是伪代码

<ul>

    <asp:repeater>

        <ItemTemplate>

            <li><img url="" /></li>

        </ItemTemplate>

    </asp:repeater>

</ul>

然后,你只要用css把li搞成横向显示即可。


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存