I have a problem connecting my database (created in SQL Server 2008 R2 Express) with c# in vs 2013

Here is the code I wrote

string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ToString();
SqlConnection conn = new SqlConnection(connStr);

the error is NullReferenceException

有帮助吗?

解决方案 2

Where did you define your connection string? In the app.config? Please send us the place where you configure this.

See http://msdn.microsoft.com/en-us/library/ms254494(v=vs.110).aspx for help.

See http://www.connectionstrings.com/ how to configure a connection string for .net

其他提示

Change this:

string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ToString();

to...

string connStr = ConfigurationManager.ConnectionStrings["newSchool"].ConnectionString;

Cheers -

I assume that there is no connection string named newSchool. It is probably NewSchool or some other casing.


In response to your comment: You need to provide a proper connection string. The ConfigurationManager is used to get a value from the configuration. The NullReferenceException shows you that the setting you request is not there.

You need to create a connection string first using the settings manager and then you can access it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top