I am creating a windows application using Visual Studio 2012 and using its built in SQL Server, I have created the database and tables but unable to access the database. I am trying to insert values in tables using insert query but it is showing error Ünable to connect to server
The connection string that I am using is
<connectionStrings>
<add name="GRTU_Library_Management.Properties.Settings.GRTU_Library_ManagementConnectionString"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory| \GRTU_Library_Management.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
and the query that I am trying is
SqlConnection con = new SqlConnection("GRTU_Library_Management.Properties.Settings.GRTU_Library_ManagementConnectionString");
con.Open();
SqlCommand cmd = new SqlCommand("insert into Category values('" + txtcategory.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
please help me in this regard,
Thanks in Advance
There's no constructor of the SqlConnection
class that accepts the name of a connection string.
If you want to retrieve a connection string from a config file, you have to do it yourself, maybe via the ConfigurationManager.ConnectionStrings
.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["GRTU_Library_Management.Properties.Settings.GRTU_Library_ManagementConnectionString"].ConnectionString);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.