C#中出现问题:“System.Data.SqlClient.SqlException”类型的异常在 System.Data.dll 中发生

问题:“System.Data.SqlClient.SqlException”类型的异常在System.Data.dll中发生,但未在用户代码中进行处理其他信息:数据类型... 问题:“System.Data.SqlClient.SqlException”类型的异常在 System.Data.dll 中发生,但未在用户代码中进行处理其他信息: 数据类型 ntext 和 varchar 在 equal to 运算符中不兼容。出现错误的代码是: public SqlDataReader getread(string M_str_sqlstr) { SqlConnection sqlcon = getcon(); SqlCommand sqlcom = new SqlCommand(M_str_sqlstr, sqlcon); sqlcon.Open(); SqlDataReader sqlread = sqlcom.ExecuteReader(CommandBehavior.CloseConnection); return sqlread; }急急急!想知道解决方法!谢谢各位! 展开
 我来答
链家大表哥
2018-06-19 · 超过24用户采纳过TA的回答
知道答主
回答量:36
采纳率:88%
帮助的人:10.9万
展开全部

下面是我们用的连接数据库的代码,直接拷过去把。引用下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;

}

}

}

}

}

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式