无法连接到SQL Server,由于的NullReferenceException [英] Cannot connect to Sql server due to NullReferenceException
问题描述
我有一个与登录表单
登录表单开始Windows窗体应用程序已被罚款,过去几年一天,我正在申请
的休息,我得到现在一个错误,
我有两个数据库中的一个DB.mdf和一个MYD.sdf
I have a windows forms application which begins with the Login form The Login form has been fine for past few day while i was working on rest of the application I get an error now that I have two database one DB.mdf and one MYD.sdf
的NullReferenceException是未处理
NullReferenceException was unhandled
对象引用未设置到对象的实例。
Object reference not set to an instance of an object.
有关代码这个特殊行--->
for this particular lines of code --- >
private void button1_Click(object sender, EventArgs e)
{
string path=@"C:\Users\Srinath\Documents\Visual Studio 2010\Projects\TESTFEE\TESTFEE\DB.mdf";
SqlConnection con =new SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDbFilename='"+path+"';User Instance=True");
string constring=ConfigurationManager.ConnectionStrings["ConnectionStringFMS"].ConnectionString;
//SqlConnection con=new SqlConnection(constring);
SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) from LOGIN where USERNAME='" + textUser.Text + "' and PASSWORD='" + textPass.Text + "'", con);
DataTable dt = new DataTable();
try
{
sda.Fill(dt);
if (dt.Rows[0][0].ToString() == "1")
{
this.Hide();
e1.Show();
}
else
{
HoldButton();
MessageBox.Show("Please Enter Your Right Credentials");
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
}//![The error i get ][1 -]
我使用的配置文件的连接字符串
试过之前,我直接用SqlConnection的连接
我使用SQL Server 2008 R2在管理工作室
我第一次收到了无法连接到默认的数据库inititally
疑惑 - >
是不是因为使用两种不同类型的分贝在一个应用程序
的问题我试着重新安装的SQL Server 2008,但没有使用
请帮忙
I tried using the Configuration File for connection string before i directly used the SqlConnection for connection I am using Sql server 2008 r2 with the Management studio I first recieved the Failed to connect to the default database inititally Doubts - > is it the Problem because of using two different types of db in one application I tried reinstalling sql server 2008 but no use please help
推荐答案
把你的连接字符串中webconfig,如果你有在*密度纤维板放在App_Data文件夹,使用此格式的作品
Put your connection string in webconfig, If you have the *.mdf placed in App_Data folder, using this format works
<connectionStrings>
<add name="ConnectionName"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|DatabaseName.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
这篇关于无法连接到SQL Server,由于的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!