C#中出现问题:“System.Data.SqlClient.SqlException”类型的异常在 System.Data.dll 中发生
下面是我们用的连接数据库的代码,直接拷过去把。引用下MYsql那个dll就行了。
至于怎么改连接的用户名和密码什么的你应该知道吧。就是下面这个图片。
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace MySQLNew
{
public class MysqlNew
{
private string _strServerAddress;
/// <summary>
/// MySQL服务器地址
/// </summary>
public string strServerAddress
{
get
{
return _strServerAddress;
}
set
{
this._strServerAddress = value;
}
}
private string _strServerPort;
/// <summary>
/// MySql服务器端口
/// </summary>
public string strServerPort
{
get
{
return _strServerPort;
}
set
{
this._strServerPort = value;
}
}
private string _strServerUser;
/// <summary>
/// MySql服务器用户名
/// </summary>
public string strServerUser
{
get
{
return _strServerUser;
}
set
{
this._strServerUser = value;
}
}
private string _strServerPwd;
/// <summary>
/// MySql服务器密码
/// </summary>
///
public string strServerPwd
{
get
{
return _strServerPwd;
}
set
{
this._strServerPwd = value;
}
}
private string _strServerDatabase;
/// <summary>
/// MySql服务器数据库
/// </summary>
///
public string strServerDatabase
{
get
{
return _strServerDatabase;
}
set
{
this._strServerDatabase = value;
}
}
private string strConnectionString;
object objLocker = new object();
public MysqlNew()
{
this.strServerAddress = "localhost";
this.strServerPort = "3306";
this.strServerUser = "root";
this.strServerPwd = "root";
this.strServerDatabase = "test";
strConnectionString = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false;port={4}",
strServerAddress, strServerUser, strServerPwd, strServerDatabase, strServerPort);
}
public int ExecuteSql(string sCmd)
{
return this._ExecuteSql(sCmd);
}
private int _ExecuteSql(string sCmd)
{
lock (objLocker)
{
try
{
using (MySqlConnection mCnn = new MySqlConnection(strConnectionString))
{
mCnn.Open();
MySqlCommand cmd = new MySqlCommand(sCmd, mCnn);
int i = cmd.ExecuteNonQuery();
mCnn.Close();
mCnn.Dispose();
return i;
}
}
catch
{
return 0;
}
}
}
public DataTable GetDataTableByCmd(string sCmd, out string sErrMsg)
{
return _GetDataTableByCmd(sCmd, out sErrMsg);
}
private DataTable _GetDataTableByCmd(string sCmd, out string sErrMsg)
{
lock (objLocker)
{
try
{
using (MySqlConnection mCnn = new MySqlConnection(strConnectionString))
{
mCnn.Open();
string sSql = sCmd;
MySqlDataAdapter adpt = new MySqlDataAdapter(sSql, mCnn);
DataTable mTable = new DataTable();
adpt.Fill(mTable);
mCnn.Close();
mCnn.Dispose();
sErrMsg = "";
return mTable;
}
}
catch (Exception err)
{
sErrMsg = err.Message;
return null;
}
}
}
public DataTable GetDataTable(string tableName, out string sErrMsg)
{
return _GetDataTable(tableName, out sErrMsg);
}
private DataTable _GetDataTable(string tableName, out string sErrMsg)
{
lock (objLocker)
{
try
{
using (MySqlConnection mCnn = new MySqlConnection(strConnectionString))
{
mCnn.Open();
string sSql = string.Format("select * from {0}", tableName);
MySqlDataAdapter adpt = new MySqlDataAdapter(sSql, mCnn);
DataTable mTable = new DataTable();
adpt.Fill(mTable);
mCnn.Close();
mCnn.Dispose();
sErrMsg = "";
return mTable;
}
}
catch (Exception err)
{
sErrMsg = err.Message;
return null;
}
}
}
}
}