我是个新手,现在在连接数据库上出现了问题,但我不知道怎么解决,请各位高人给予指点,先谢过了。
具体问题是这样的:
具体问题是这样的: 我在SQL SERVER 2008中新建了一个数据库(名称是EXPERIMENT),然后在这个数据库中建立了一张空表(此表只有列名没有任何数据),名为EXPERMENT_TABLE;
然后我使用VS 2010进行程序化的连接,在这个过程中,我按如下步骤进行操作; 1.点击VS 2010界面上的 数据-->添加数据源-->(选择数据库)-->下一步-->(选择数据集)-->下一步-->(指定了服务器名和要连接的数据库名称)-->(测试连接通过)-->确定-->下一步-->(在之后保存连接字符串提示的界面中,我对“是”选项打钩)-->下一步-->(在选择数据库对象的界面中,我对“表”选项打钩)-->完成。
此时,在vs 2010 右边的解决方案管理器中,生成了app.config文件。
2.双击VS 2010中Form1设计窗口上的button1,进入Form1.cs界面,在此文件头加上了 using System.Configuration;和 using System.Data.SqlClient;
然后写入如下代码:
private void button1_Click(object sender, EventArgs e)
{
string theconnectionstring = ConfigurationManager.ConnectionStrings ["MainConnection"].ToString();
SqlConnection ConnectNow = new SqlConnection(theconnectionstring);
ConnectNow.Open();
if (ConnectNow.State == System.Data.ConnectionState.Open)
label1.Text = "Database connection is opening!";
}
但是,此代码编译执行以后却发生了异常。VS的提示如下:
************** 异常文本 **************
System.NullReferenceException: 未将对象引用设置到对象的实例。
在 WindowsFormsApplication3.Form1.button1_Click(Object sender, EventArgs e) 位置 c:\documents and settings\administrator\my documents\visual studio 2010\Projects\WindowsFormsApplication3\WindowsFormsApplication3\Form1.cs:行号 23
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** 已加载的程序集 **************
mscorlib
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.235 (RTMGDR.030319-2300)
基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
WindowsFormsApplication3
程序集版本: 1.0.0.0
Win32 版本: 1.0.0.0
基本代码: file:///c:/documents%20and%20settings/administrator/my%20documents/visual%20studio%202010/Projects/WindowsFormsApplication3/WindowsFormsApplication3/bin/Debug/WindowsFormsApplication3.exe
----------------------------------------
System.Windows.Forms
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.235 built by: RTMGDR
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.1 built by: RTMRel
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.236 built by: RTMGDR
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Configuration
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.1 built by: RTMRel
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Data
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
System.Core
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.1 built by: RTMRel
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
mscorlib.resources
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.235 (RTMGDR.030319-2300)
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Windows.Forms.resources
程序集版本: 4.0.0.0
Win32 版本: 4.0.30319.235 built by: RTMGDR
基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------
************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。
例如:
启用 JIT 调试后,任何未经处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。
******************************************************************
到底问题是出在哪里呢?
希望高人指点,在此先谢谢了。