在执行调试(不设断点)时报错:System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常 。
声明:在调试设断点时运行时正常的。
在执行不调试时报错:
************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 jingxiang.ConfigFrm.Open(String strIP, String strDatabase, String strUid, String strPwd)
位置 E:\Office\Task 2667\jingxiang\jingxiang\ConfigFrm.cs:行号 576
在 jingxiang.ConfigFrm.btnDTest_Click(Object sender, EventArgs e)
位置 E:\Office\Task 2667\jingxiang\jingxiang\ConfigFrm.cs:行号 595 请高手帮忙看看这是什么原因导致的,谢谢。 主要代码如下: private void Open(string strIP, string strDatabase, string strUid, string strPwd)
{
string connectionString = "Data Source=" + strIP + "; Initial Catalog=" + strDatabase + "; Persist Security Info=True; User ID=" + strUid + "; Pwd=" + strPwd;
SqlConnection mySqlConnection = new SqlConnection(connectionString);×××执行调试(不设断点)时报错位置
try
{
if(mySqlConnection.State==ConnectionState.Closed)
mySqlConnection.Open();
}
catch (Exception err)
{
MessageBox.Show("不能联接" + err.ToString(), "出错了!", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
finally
{
if (mySqlConnection.State == ConnectionState.Closed || mySqlConnection.State == ConnectionState.Broken)
{
MessageBox.Show("连接失败");
}
else
{
mySqlConnection.Close();
MessageBox.Show("连接成功");
}
}
}
声明:在调试设断点时运行时正常的。
在执行不调试时报错:
************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 jingxiang.ConfigFrm.Open(String strIP, String strDatabase, String strUid, String strPwd)
位置 E:\Office\Task 2667\jingxiang\jingxiang\ConfigFrm.cs:行号 576
在 jingxiang.ConfigFrm.btnDTest_Click(Object sender, EventArgs e)
位置 E:\Office\Task 2667\jingxiang\jingxiang\ConfigFrm.cs:行号 595 请高手帮忙看看这是什么原因导致的,谢谢。 主要代码如下: private void Open(string strIP, string strDatabase, string strUid, string strPwd)
{
string connectionString = "Data Source=" + strIP + "; Initial Catalog=" + strDatabase + "; Persist Security Info=True; User ID=" + strUid + "; Pwd=" + strPwd;
SqlConnection mySqlConnection = new SqlConnection(connectionString);×××执行调试(不设断点)时报错位置
try
{
if(mySqlConnection.State==ConnectionState.Closed)
mySqlConnection.Open();
}
catch (Exception err)
{
MessageBox.Show("不能联接" + err.ToString(), "出错了!", MessageBoxButtons.OK, MessageBoxIcon.Question);
}
finally
{
if (mySqlConnection.State == ConnectionState.Closed || mySqlConnection.State == ConnectionState.Broken)
{
MessageBox.Show("连接失败");
}
else
{
mySqlConnection.Close();
MessageBox.Show("连接成功");
}
}
}
解决方案 »
- 有没有人懂得“增强现实”软件的开发情况!比如用什么语言开发?要用到什么技术?用到那些知识?(不要从百度复制粘贴,说些我不知道的)
- MSSQL数据库中有个字段类型为Image C#中应该用什么类型?
- 怎样循环加载TextBox 的KeyDown事件,并在按下回车后执行下一步
- 如果要更改普通的textboxcell 变成 DataGridViewComboBoxCell ,有什么办法可以实现呢?
- winform 界面覆盖和界面分离
- 一个小问题?请大家进来帮帮我?
- c#中如何得到一个字符的ASCII值?
- 小问题
- 如何自定义编辑TreeView节点时编辑框的初始值?
- web页面,一个Table的<td>中如何让关两个控件重叠啊?
- vs2005 怎么添加flash?
- 输入10个数,求出最大值和最小值
如你所说,如果这里的话,可能是你的字符串有问题,另外可能执行此方法后字符串更改,而再次执行时候出错。或者是调用方法时候有循环,很容易出错
问题终于解决了,程序出错的原因应该是:我在修改程序后没有重新生成项目,只重新生成了解决方案, 把根目录下的一系列项目生成的文件手工删除后,在执行程序就没问题了。好像这段更简单实用些:
SqlConnection myConnection = null;
try
{
string connString = "server="+strIP+";database="+strDatabase+";uid="+strUid+";pwd="+strPwd+";";
myConnection = new SqlConnection(connString);
if (myConnection.State == ConnectionState.Closed)
myConnection.Open();
myConnection.Close();
myConnection.Dispose();
MessageBox.Show(" 连接成功 ");
}
catch (Exception)
{
throw;
}小弟,谢过了 ...