“System.Data.SqlClient.Sqlsystem.exception异常”类型的未经处理的异常在 System.Data.dll 中发生

System.AccessViolationException类型的未经处理的异常在System.Data.dll中发 - .NET综合 - 编程入门网
System.AccessViolationException类型的未经处理的异常在System.Data.dll中发
System.AccessViolationException&类型的未经处理的异常在 System.Data.dll 中发生。其他信息:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
错误背景:
操作系统:编程环境:VS2013; &语言:VB.net; &数据库:SQLserver2008
做数据库连接时,发生的错误:
错误提示为:
说明:用VB.net连接SQLServer数据库
第一种情况:
连接字符串为&Server=(Local);Database=charge_UserID =Password=123456&时,连接没问题;
第二种情况:
当把& Server=(Local)&换为 & Server=192.168.24.123 &,即把local换为数据库服务器IP地址时,连接不成功,出现如上错误(PS:其他人的机器上就可以)。
第一种情况的代码:
Public Class UserDAO
Public connStr As String = &Server=(Local);Database=charge_User ID =Password=123456&
Public Function UserLogin(ByVal userName As String, ByVal password As String) As Boolean
&span style=&white-space:pre&&
Dim conn As New SqlConnection(connStr)
Dim dbcmd As New SqlCommand
Dim reader As SqlDataReader
&span style=&white-space:pre&& &/span&
Dim sqlStr As String = &select * from User_Info where userID=@userName and PWD=@password&
'与数据库建立连接
conn.Open()
'操作数据库
dbcmd = New SqlCommand(sqlStr, conn)
dbcmd.Parameters.Add(New SqlParameter(&@userName&, userName))
dbcmd.Parameters.Add(New SqlParameter(&@password&, password))
reader = dbcmd.ExecuteReader
If reader.Read Then
Return True
Return False
'关闭与数据库的连接
conn.Close()
End Function
第二种情况的代码
Public Class UserDAO
Public connStr As String = &Server=192.168.24.123;Database=charge_User ID =Password=123456&
Public Function UserLogin(ByVal userName As String, ByVal password As String) As Boolean
Dim conn As New SqlConnection(connStr)
Dim dbcmd As New SqlCommand
Dim reader As SqlDataReader
&span& &/span&
Dim sqlStr As String = &select * from User_Info where userID=@userName and PWD=@password&
'与数据库建立连接
conn.Open()
'操作数据库
dbcmd = New SqlCommand(sqlStr, conn)
dbcmd.Parameters.Add(New SqlParameter(&@userName&, userName))
dbcmd.Parameters.Add(New SqlParameter(&@password&, password))
reader = dbcmd.ExecuteReader
If reader.Read Then
Return True
Return False
'关闭与数据库的连接
conn.Close()
End Function
两种代码的不同之处仅仅在于数据库连接字符串中的Server值不同。列名“”无效解决方法
&来源:读书人网&【读书人网():综合教育门户网站】
列名“”无效protected void LoadMessage(string str1, string str2){SqlConnection con
new SqlConnectio
列名“”无效protected void LoadMessage(string str1, string str2) &
SqlConnection con = new SqlConnection(strConstrin); &
con.Open(); &
sqlString = &select * from Student where & + str1 + &=& + str2 + &&; &
SqlCommand cmd = new SqlCommand(sqlString, con); &
SqlDataAdapter SqlAd = new SqlDataAdapter(cmd); &
DataSet Rs = new DataSet(); &
SqlAd.Fill(Rs); &
DataTable NewsTable = Rs.Tables[0]; &
this.GridView1.DataSource = NewsT &
this.GridView1.DataBind(); &
con.Close(); &
} &protected void Button1_Click(object sender, EventArgs e) &
string name = TextBox1.T &
LoadMessage(&Name&, name); &
}输入textbox的值后,总是提示列名 '黄生' 无效。& 说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。& 异常详细信息: System.Data.SqlClient.SqlException: 列名 '武凯亮' 无效。源错误:& 行 42:
SqlDataAdapter SqlAd = new SqlDataAdapter(cmd);行 43:
DataSet Rs = new DataSet();行 44:
SqlAd.Fill(Rs);行 45:
DataTable NewsTable = Rs.Tables[0];行 46:
this.GridView1.DataSource = NewsT &源文件: c:\Users\Administrator\Desktop\学生信息管理系统\Maneger.aspx.cs
行: 44& 堆栈跟踪:& [SqlException (0x): 列名 '武凯亮' 无效。] &
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +2062238 &
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5050268 &
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234 &
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2275 &
System.Data.SqlClient.SqlDataReader.ConsumeMetaData() +33 &
System.Data.SqlClient.SqlDataReader.get_MetaData() +86 &
System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +311 &
System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +987 &
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162 &
System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32 &
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +141 &
System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) +12 &
mon.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +10 &
mon.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +144 &
mon.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +319 &
mon.DbDataAdapter.Fill(DataSet dataSet) +94 &
Maneger.LoadMessage(String str1, String str2) in c:\Users\Administrator\Desktop\学生信息管理系统\Maneger.aspx.cs:44 &
Maneger.Button1_Click(Object sender, EventArgs e) in c:\Users\Administrator\Desktop\学生信息管理系统\Maneger.aspx.cs:59 &
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112 &
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 &
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 &
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 &
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563 &[解决办法]你既然就是要根据名称查询,就没必要传两个参数进去protected void LoadMessage(string str1) { SqlConnection con = new SqlConnection(strConstrin); con.Open(); sqlString = &select * from Student where Name='& + str2 + &'&; SqlCommand cmd = new SqlCommand(sqlString, con); SqlDataAdapter SqlAd = new SqlDataAdapter(cmd); DataSet Rs = new DataSet(); SqlAd.Fill(Rs); DataTable NewsTable = Rs.Tables[0]; this.GridView1.DataSource = NewsT this.GridView1.DataBind(); con.Close(); } &protected void Button1_Click(object sender, EventArgs e) {
string name = TextBox1.T LoadMessage(name);
}最后强烈建议参数化查询,防止sql注入[解决办法]
sqlString = &select * from Student where & + str1 + &=& + str2 + &&;错了,把它改为:sqlString = &select * from Student where & + str1 + &='& + str2 + &'&;因为你输入的是字符串,并且你定义的那个“Name”应该是varchar类型的吧。正确的代码运行调试时,代码如下:select * from Student where Name='name'这样是把问题解决了,但是还有点不足就是如六楼所说的,为了防止sql注入,建议使用参数化查询,别用这种方法。15:54 提问
求解!!“System.Data.SqlClient.SqlException”类型的异常
使用vs调试网站时候出现一下问题。
“System.Data.SqlClient.SqlException”类型的异常在 System.Data.dll 中发生,但未在用户代码中进行处理
其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL Network Interfaces, error: 26 - 定位指定的服务器/实例时出错)
using System.Collections.G
using System.L
using System.W
using System.Web.UI;
using System.Web.UI.WebC
using System.D
using System.Data.SqlC
public partial class UserController_top : System.Web.UI.UserControl
protected void Page_Load(object sender, EventArgs e)
if (!this.IsPostBack)
/// &summary&
/// 按照影片的点击排行,
/// &/summary&
public void Bind()
string str = "select top 10 * from Movies order by MovieHit desc";
SqlConnection con = DB.getConnection();
DataSet ds = new DataSet(); //多行数据填充到数据集中
SqlDataAdapter sda = new SqlDataAdapter(str, con);
sda.Fill(ds, "Movie");
this.DataList1.DataSource = ds.Tables["Movie"].DefaultV
DataList1.DataBind();
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
if (e.CommandName == "Play"){
string hit = (e.Item.FindControl("Label8") as Label).T
string str = (e.Item.FindControl("Lable20") as Label).T
int id = Convert.ToInt32(str); //影片的ID号
int num = Convert.ToInt32(hit) + 1; //以前的播放次数加上1就为当前的播放次数
SqlConnection con = DB.getConnection();
con.Open();
SqlCommand cmd = new SqlCommand("update Movies set MovieHit=" + num + " where MovieID=" + id + "", con);
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Play.aspx?ID=" + id);
问题代码:
sda.Fill(ds, "Movie");
软件版本:visualstudio2013+mysql5.6
按赞数排序
你检查一下变量 con ,可能是数据库没有正确的连接!
删除数据库后重新连接还是不行。
主要问题可能出现在你的DB.getConnection();方法里,最好到里面找找看,是不是ConnectionString有问题,还是服务器有问题
其他相似问题红色的为报错的地方,找个好久不知道怎么改,服务启动了,允许远程连接&/&VS2015&&SQL&2012
&public&void&DLClassBind(DataList&dlName)
&&&&&&&&string&P_Str_SqlStr&=&&select&*&from&Class&;
&&&&&&&&SqlConnection&myConn&=&dbObj.GetConnection();
&&&&&&&&SqlDataAdapter&da&=&new&SqlDataAdapter(P_Str_SqlStr,&myConn);
&&&&&&&&DataSet&ds&=&new&DataSet();
&&&&&&&&da.Fill(ds,&&Class&);
&&&&&&&&dlName.DataSource&=&ds.Tables[&Class&].DefaultV
&&&&&&&&dlName.DataBind();
&&&&///&&summary&
&&&&///&绑定图书信息(精品推荐 热销图书&打折图书)
&&&&///&&/summary&
&&&&///&&param&name=&P_Int_Deplay&&(精品推荐&热销图书&打折图书)三种类别的标志&/param&
&&&&///&&param&name=&P_Str_srcTable&&表信息&/param&
&&&&///&&param&name=&DLName&&绑定控件名&/param&
&&&&public&void&DGIBind(int&P_Int_Deplay,&string&P_Str_srcTable,&DataList&DLName)
&&&&&&&&SqlConnection&myConn&=&dbObj.GetConnection();
&&&&&&&&SqlCommand&myCmd&=&new&SqlCommand(&Pr_DeplayBookInfo&,&myConn);
&&&&&&&&mandType&=&CommandType.StoredP
&&&&&&&&//添加参数
&&&&&&&&SqlParameter&Deplay&=&new&SqlParameter(&@Deplay&,&SqlDbType.Int,&4);
&&&&&&&&Deplay.Value&=&P_Int_D
&&&&&&&&myCmd.Parameters.Add(Deplay);
&&&&&&&&//执行过程
&&&&&&&&myConn.Open();
&&&&&&&&try
&&&&&&&&&&&&myCmd.ExecuteNonQuery();
&&&&&&&&catch&(Exception&ex)
&&&&&&&&&&&&throw&(ex);
&&&&&&&&finally
&&&&&&&&&&&&myCmd.Dispose();
&&&&&&&&&&&&myConn.Close();
&&&&&&&&SqlDataAdapter&da&=&new&SqlDataAdapter(myCmd);
&&&&&&&&DataSet&ds&=&new&DataSet();
&&&&&&&&da.Fill(ds,&P_Str_srcTable);
&&&&&&&&DLName.DataSource&=&ds.Tables[P_Str_srcTable].DefaultV
&&&&&&&&DLName.DataBind();
回复讨论(解决方案)
贴更多的错误信息,看你的连接字符串怎么写的。测试你的数据库是否真的能够远程连接了
翻了一下老帖子.提问前利用论坛的搜索功能
“System.Data.SqlClient.SqlException”类型的异常在&System.Data.dll&中发生,但未在用户代码中进行处理
其他信息:&在与&SQL&Server&建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且&SQL&Server&已配置为允许远程连接。&(provider:&SQL&网络接口,&error:&26&-&定位指定的服务器/实例时出错)用户代码未处理&System.Data.SqlClient.SqlException
&&Class=20
&&ErrorCode=-
&&LineNumber=0
&&Message=在与&SQL&Server&建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且&SQL&Server&已配置为允许远程连接。&(provider:&SQL&网络接口,&error:&26&-&定位指定的服务器/实例时出错)
&&Number=-1
&&Server=&&
&&Source=.Net&SqlClient&Data&Provider
&&StackTrace:
&&&&&&&在&System.Data.SqlClient.SqlInternalConnection.OnError(SqlException&exception,&Boolean&breakConnection)
&&&&&&&在&System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject&stateObj)
&&&&&&&在&System.Data.SqlClient.TdsParser.Connect(ServerInfo&serverInfo,&SqlInternalConnectionTds&connHandler,&Boolean&ignoreSniOpenTimeout,&Int64&timerExpire,&Boolean&encrypt,&Boolean&trustServerCert,&Boolean&integratedSecurity,&SqlConnection&owningObject,&Boolean&withFailover)
&&&&&&&在&System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo&serverInfo,&String&newPassword,&Boolean&ignoreSniOpenTimeout,&Int64&timerExpire,&SqlConnection&owningObject,&Boolean&withFailover)
&&&&&&&在&System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String&host,&String&newPassword,&Boolean&redirectedUserInstance,&SqlConnection&owningObject,&SqlConnectionString&connectionOptions,&Int64&timerStart)
&&&&&&&在&System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection&owningObject,&SqlConnectionString&connectionOptions,&String&newPassword,&Boolean&redirectedUserInstance)
&&&&&&&在&System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity&identity,&SqlConnectionString&connectionOptions,&Object&providerInfo,&String&newPassword,&SqlConnection&owningObject,&Boolean&redirectedUserInstance)
&&&&&&&在&System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions&options,&Object&poolGroupProviderInfo,&DbConnectionPool&pool,&DbConnection&owningConnection)
&&&&&&&在&System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection&owningConnection,&DbConnectionPool&pool,&DbConnectionOptions&options)
&&&&&&&在&System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection&owningObject)
&&&&&&&在&System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection&owningObject)
&&&&&&&在&System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection&owningObject)
&&&&&&&在&System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection&owningConnection)
&&&&&&&在&System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection&outerConnection,&DbConnectionFactory&connectionFactory)
&&&&&&&在&System.Data.SqlClient.SqlConnection.Open()
&&&&&&&在&UserInfoClass.DGIBind(Int32&P_Int_Deplay,&String&P_Str_srcTable,&DataList&DLName)&位置&h:\asp.net\大作业\BookShop\App_Code\UserInfoClass.cs:行号&385
&&&&&&&在&index.RefineBind()&位置&h:\asp.net\大作业\BookShop\User\index.aspx.cs:行号&80
&&&&&&&在&index.Page_Load(Object&sender,&EventArgs&e)&位置&h:\asp.net\大作业\BookShop\User\index.aspx.cs:行号&21
&&&&&&&在&System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr&fp,&Object&o,&Object&t,&EventArgs&e)
&&&&&&&在&System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object&sender,&EventArgs&e)
&&&&&&&在&System.Web.UI.Control.OnLoad(EventArgs&e)
&&&&&&&在&System.Web.UI.Control.LoadRecursive()
&&&&&&&在&System.Web.UI.Page.ProcessRequestMain(Boolean&includeStagesBeforeAsyncPoint,&Boolean&includeStagesAfterAsyncPoint)
&&InnerException:&
你认真看我回复的里连接的帖子了吗?认真看你的错误提示了吗?那么明显的告诉你数据库连接出错了,你还不知道去哪里找问题吗?像你这样的问题,在百度一搜就解决掉的。多看看书,多看看别人的源代码,多和同学交流讨论。
连接字符串错误,你用这个连接字符串去看,肯定连不上数据库,你改一下连接字符串}

我要回帖

更多关于 c system.exception 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信