Skip to main content
 首页 » 编程设计

c#之从 C# 连接到 SQL Server Express

2024年02月13日12birdshome

我尝试打开与 SQL Server Express 的连接并在 C# 中的特定表上分配新记录。

这段代码给我这个错误

ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

以及以下内容:

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)

我的代码:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Data.SqlClient; 
 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            SqlConnection myConnection = new SqlConnection("Server=HABCHY-PC/SQLEXPRESS;" + 
                                    "Trusted_Connection=yes;" + 
                                    "Database=mydatabase;" + 
                                    "User Instance=true;"+ 
                                    "Connection timeout=30"); 
            try 
            { 
                myConnection.Open(); 
            } 
            catch (Exception e) 
            { 
                Console.WriteLine(e.ToString()); 
            }  
 
            SqlCommand myCommand = new SqlCommand("INSERT INTO students (firstname, age) "+ 
                    "Values ('string', 1)", myConnection); 
            myCommand.ExecuteNonQuery(); 
 
            try 
            { 
                SqlDataReader myReader = null; 
                myCommand.CommandText = "select * from students"; 
                myReader = myCommand.ExecuteReader(); 
 
                while (myReader.Read()) 
                { 
                    Console.WriteLine(myReader["firstname"].ToString()); 
                    Console.WriteLine(myReader["age"].ToString()); 
                } 
            } 
            catch (Exception e) 
            { 
                Console.WriteLine(e.ToString()); 
            } 
 
            try 
            { 
                myConnection.Close(); 
            } 
            catch (Exception e) 
            { 
                Console.WriteLine(e.ToString()); 
            } 
        } 
    } 
} 

请告诉我问题是什么。

请您参考如下方法:

我认为这对你有用:

using System; 
using System.Linq; 
using System.Data.SqlClient; 
 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
        public static SqlConnection GetConnectionsString() 
        { 
            return new SqlConnection("Server=HABCHY-PC\\SQLEXPRESS;" + 
                                       "Trusted_Connection=true;" + 
                                       "Database=mydatabase;" + 
                                       "User Instance=true;" + 
                                       "Connection timeout=30"); 
        } 
 
        static void Main(string[] args) 
        { 
            using (SqlConnection myConnection = GetConnectionsString()) 
            { 
                try 
                { 
                    SqlCommand myCommand = new SqlCommand("INSERT INTO students (firstname, age) " + "Values ('string', 1)", myConnection); 
                    Console.WriteLine("ee"); 
 
                    myConnection.Open(); 
 
                    myCommand.ExecuteNonQuery(); 
 
                    SqlDataReader myReader = null; 
                    myCommand.CommandText = "select * from students"; 
                    myReader = myCommand.ExecuteReader(); 
                    while (myReader.Read()) 
                    { 
                        Console.WriteLine(myReader["firstname"].ToString()); 
                        Console.WriteLine(myReader["age"].ToString()); 
                    } 
 
                 } 
                 catch (Exception e) 
                 { 
                     Console.WriteLine(e.ToString()); 
                 } 
             } 
         } 
     } 
 } 

如您所见,我在另一种方法中提取了 SQLConnection。 当您以后想要发出其他数据库请求时,这将对您有所帮助。 使用 using block ,您可以确保即使您的请求出现异常,您的连接也会关闭。