连接到SQL Server数据库内Visual Studio 2012 C#

连接到SQL Server数据库内Visual Studio 2012 C#

问题描述:

原谅我,如果这很容易,我看到类似的帖子,但我是新的到C#和一直在努力,所以任何帮助将非常感激。

Forgive me if this is easy and I have seen similar posts but I am new-ish to C# and have been struggling on this, so any help would be much appreciated.

我在Visual Studio 2012中尝试连接到本地DB SQL Server,但到目前为止没有运气。

I am trying to connect to a local DB SQL Server in Visual Studio 2012 but so far have had no luck.

我从我的连接字符串属性

I got my connection string from the properties of my local DB which in the picture is in the left hand pane.

public void connection()
{

    string connectionString = "Data Source=(localdb)\v11.0;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";

    SqlConnection con = new SqlConnection(connectionString);

    try
    {
        con.Open();
        lblConnectionTest.Text = "Connected successfully";

    }
    catch (SqlException ex)
    {
        lblConnectionTest.Text = ex.Message;
    }


} 

,所有我想做的是建立一个连接到DB,基本上写出连接成功等,如果它连接。

At this point, all I am trying to do is establish a connection to the DB and basically write out "connection successful" etc if it connects.

目前使用的是什么,我收到以下错误:

Currently with what I have, I receive the following error:

A network-related or instance-specific error occurred 
while establishing a connection to SQL Server. 
The server was not found or was not accessible. 
Verify that the instance name is correct and that SQL Server 
is configured to allow remote connections. 
(provider: Named Pipes Provider, error: 40 -     
Could not open a connection to SQL Server)

我在这里做错了什么?

非常感谢!

如果你是在C#,你可以创建一个应用程序配置文件(App.config),它将有连接字符串与其名称。

If you are in C#, you can create an application config file (App.config), which will be having the connection string with its name.

这是示例app.config中的代码

this is sample code in the app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
    <connectionStrings>
       <add name="myConnString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\AppsTest\Nov17RoomBooking\dbRoomBooking.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

然后,您的代码应如下所示:

Then Your code should look like this:

private static string sqlConnectionString = ConfigurationManager.ConnectionStrings["myConnString"].ConnectionString;

    private void DeletingDataBase()
    {
        using (SqlConnection sqlConn = new SqlConnection(sqlConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                string sqlQuery = "SELECT, INSERT, UPDATE, DELETE";
                cmd.Connection = sqlConn;
                cmd.CommandText = sqlQuery;
                try
                {
                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                }
                catch { }
                finally
                {
                    cmd.Connection.Close();
                }
            }
        }
    }