代码之家  ›  专栏  ›  技术社区  ›  yoav.str

使用.NETC处理数据库#

  •  1
  • yoav.str  · 技术社区  · 14 年前

    过路费->连接到数据库 并宣布他将创建新的数据库

          try
            {
                // step 1: create a SqlConnection object to connect to the
                // SQL Server my server connection string proprties  database
        // i think the bug should be here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                SqlConnection mySqlConnection = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Users/STERN/Documents/myDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");//"server=localhost;database=Northwind;uid=sa;pwd=sa");
                // step 2: create a SqlCommand object
                SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
                // step 3: set the CommandText property of the SqlCommand object to
                // a SQL SELECT statement that retrieves a row from the Customers table
                mySqlCommand.CommandText =
                "SELECT type " +
                "FROM Table1 " +
                "WHERE type = ‘ALFKI’";
                // step 4: open the database connection using the
                // Open() method of the SqlConnection object
                mySqlConnection.Open();
    

    /* 一些代码。。。。 */

    catch (SqlException e)
            {
                Console.WriteLine("A SqlException was thrown");
                Console.WriteLine("Number = " + e.Number);
                Console.WriteLine("Message = " + e.Message);
                Console.WriteLine("StackTrace:\n" + e.StackTrace);
            }
            string s = Console.ReadLine();
        }
    

    我得到的唯一例外就是被抛弃 mySqlConnection.Open(); 这是我收到的错误消息:

    数字=3 Message=建立时发生与网络相关或特定于实例的错误 正在断开与SQL Server的连接。找不到服务器或无法访问服务器。v 远程连接(提供程序:命名管道提供程序,错误:40-无法操作 (连接到SQL Server) 堆栈跟踪: ,布尔断开连接) 位于System.Data.SqlClient.TdsParser.throweexception和warning()处 位于System.Data.SqlClient.TdsParser.Connect(ServerInfo ServerInfo,SqlInternal ConnectionDS connHandler,布尔IgnoresInOpenTimeout,Int64 TimerSpire,Bool ean加密、布尔信任服务器证书、布尔集成安全性) 位于System.Data.SqlClient.SqlInternalConnectionDS.AttemptOneLogin(ServerInfo 位于System.Data.SqlClient.sqlinternalconnectionds.LoginNoFailover(ServerInfo serverInfo、字符串newPassword、布尔重定向edUserInstance、SqlConnection ningObject、SqlConnectionString连接选项、超时超时) 在owningObject上,TimeoutTimer timeout,SqlConnectionString connectionOptions,St newseriedunce,布尔值) ity标识、SqlConnectionString连接选项、对象提供者信息、字符串 位于System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp 拥有连接) Section owningConnection、DbConnectionPool池、DbConnectionOptions选项) 位于System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection) (对象) 在System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection)中 在System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection)中 terConnection、DbConnectionFactory(连接工厂) 在System.Data.SqlClient.SqlConnection.Open()处 在C:\Users\STERN\AppData中的ConsoleApplication1.Program.Main(字符串[]args)处\

    如果有人知道pdf,文章,一些对我有帮助的东西,他能在这里写下来会很好。。。 有人能帮帮我吗。。。。

    3 回复  |  直到 9 年前
        1
  •  1
  •   Justin Niessner    14 年前

    您似乎无法连接到SQL Server Express的本地实例。

    即使您正在指定本地数据库文件,该文件在连接后也将附加到SQL Server Express的本地实例。然后通过SQL Server Express(而不是直接从文件)通过附加的数据库完成所有数据访问。

    确保它已启动并正在运行,并且可以使用SQL Management Studio Express连接到它。

    SQL Server 2008 Connection Strings

        2
  •  0
  •   Andy_Vulhop    14 年前

    尝试在SQL Server Management Studio中使用相同的连接字符串连接到数据库。它连接吗?如果没有(我怀疑它会),您的连接字符串就错了。

    此外,请确保SQL Server和SQL代理服务正在运行。

        3
  •  0
  •   Jason Jong    14 年前

    我不得不说,应用程序在连接到SQL server时遇到问题,如果SQL连接字符串正确,请使用SQL表面积和配置检查是否允许管道名称和远程连接。

    http://support.microsoft.com/kb/914277