使用c# asp.net连接到SQL Server Express数据库
本文关键字:SQL Server Express 数据库 连接 asp net 使用 | 更新日期: 2023-09-27 18:03:30
我能够连接到网络上服务器上的数据库,没有任何问题。然而,当我尝试使用SQL Server Express(本地db)时,我一直得到同样的错误。我创建了一个新的解决方案,只有一个按钮和标签,以消除任何其他潜在的问题。我已经在stackoverflow和google中查找并尝试了几乎所有场景,但无济于事....我决定发布一个问题,因为我花了太多的时间试图解决这个问题。谢谢你的指导
错误:c#代码:对象引用未设置为对象的实例。
描述:当前web请求执行过程中出现未处理的异常。请查看堆栈跟踪以获得有关错误及其在代码中的起源位置的更多信息。
Exception Details: System。NullReferenceException:对象引用没有设置为对象的实例。
堆栈跟踪:
[NullReferenceException: Object reference not set to a instance of Object .]
DelWebApplication1.WebForm1。Button1_Click(Object sender, EventArgs e) in c:'Users'Rozelle'Documents'Visual Studio 2013'Projects'DelWebApplication1'DelWebApplication1'WebForm1.aspx.cs:23
System.Web.UI.WebControls.Button。OnClick(EventArgs e) +9628614
System.Web.UI.WebControls.Button。RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler。RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page。RaisePostBackEvent(ipostbackeventandler sourceControl, String eventArgument) +13
System.Web.UI.Page。RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page。ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Sql;
namespace DelWebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["MYConnectionString"].ToString();
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
Label1.Text = "Database Connected!!";
conn.Close();
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
}
}
网络。配置:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="DelWebApplication1.Properties.Settings.MyConnectionString"
connectionString="Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Rozelle'Documents'My.mdf;Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
Replace
<add name="DelWebApplication1.Properties.Settings.MyConnectionString" connectionString="Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Rozelle'Documents'My.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
<add name="MYConnectionString" connectionString="Data Source=(LocalDB)'v11.0;AttachDbFilename=C:'Users'Rozelle'Documents'My.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient" />
您的连接字符串名称在ConfigurationManager.ConnectionStrings["MYConnectionString"].ToString();
中有大写Y
。c#代码和Web中的连接字符串名称。