I've been looking around for a solution to this issue and the only one that might provide a solution is the only one I haven't tried, because it involves changing system properties. I want to avoid that if I can.

我一直在寻找这个问题的解决方案,唯一可能提供解决方案的是我唯一没有尝试过的方案,因为它涉及到改变系统属性。如果可以的话,我想避免。

The Connection string is correct, but it still won't connect.

连接字符串是正确的,但是它仍然不会连接。

The exception occurs on line 30, which is where the connection string is opened. conn.Open()

异常发生在第30行,这是连接字符串被打开的地方。conn.Open()

Here is the Code:

这是代码:

using System.Text;
using System.Data.SqlClient;

namespace My_Sql_Program
{
    class Program
    {
        static void Main()
        {
            try
            {
                // Step 1: Create a SqlConnection Object to connect to the 
                // SQL Northwind database.
                SqlConnection conn = new SqlConnection("server=localhost;database=dcim;uid=root;pwd=LlmD62jL");

                // Step 2: Create a SqlCommand object.
                SqlCommand cmd = conn.CreateCommand();

                // Step 3: Set the CommandText property of the SqlCommand object to a 
                // SQL SELECT statment that retrieves a row from the Customers table.
                cmd.CommandText = "SELECT UserName, UserPassword, AuthorityLevel, " +
                    "FROM Users " +
                    "WHERE UserName = 'ben'";

                // Step 4: Open the database connection using the 
                // Open() method of the SqlConnection object.
                conn.Open();

                //Step 5: Create a SqlDataReader object and call the ExecuteReader()
                //Method of the SqlCommand object to run the SELECT statement.
                SqlDataReader rdr = cmd.ExecuteReader();

                // Step 6: Read the row from the SqlDataReader object using the Read() method.
                rdr.Read();

                // Step 7: Display the column values.
                Console.WriteLine("rdr[\"UserName\"] = " + rdr["UserName"]);
                Console.WriteLine("rdr[\"UserPassword\"] = " + rdr["UserPassword"]);
                Console.WriteLine("rdr[\"AuthorityLevel\"] = " + rdr["AuthorityLevel"]);

                // Step 8: Close the SqlReader object using the Close() method.
                rdr.Close();

                // Step9: Close the SqlConnection object using the Close() method.
                conn.Close();
            }
            catch (SqlException e) 
            {
                Console.WriteLine("An SqlException was thrown.");
                Console.WriteLine("Number = " + e.Number);
                Console.WriteLine("Message = " + e.Message);
                Console.WriteLine("StackTrace:\n" + e.StackTrace);
            }
            Console.ReadLine();
        }
    }
}

And when I run it, nothing happens for about 30 seconds, then the SqlException is printed:

当我运行它时,大约30秒内什么都没有发生,然后SqlException被打印出来:

An SqlException was thrown.
Number = 2
Message = 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)

StackTrace:
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean
breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean
breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,
Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds
connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean
trustServerCert, Boolean integratedSecurity, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String
newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout,
Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String
newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString
connectionOptions, SqlCredential credential, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout,
SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString
newSecurePassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,
SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String
newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString
userConnectionOptions)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options,
DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection
owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool,
DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32
waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection,
DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,
TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection,
TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,
DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions
userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at My_Sql_Program.Program.Main() in c:\users\paul\documents\visual studio 2010\Projects\My Sql
Program\My Sql Program\Program.cs:line 30

2 个解决方案

#1


5

SqlConnection is for SqlServer. To connect to MySql you need to download some drivers and reference it in your project, and use MySqlConnection instead.

SqlConnection是。状态"置疑"要连接到MySql,需要下载一些驱动程序并在项目中引用它,然后使用MySqlConnection。

Take a look at this question and this CodeProject sample.

看看这个问题和这个CodeProject示例。

更多相关文章

  1. 关于mysql无法添加中文数据的问题以及解决方案
  2. SQL从结果数据库中选择subCode及其得分,并将其输出为一个字符串
  3. [JavaScript] 将字符串数组转化为整型数组
  4. JavaScript 字符串函数 之查找字符方法(一)
  5. 范围变量值的变化没有反映在我的字符串中
  6. Javascript字符串替换为动态创建的模式。
  7. 求助 急 js替换字符串问题
  8. 寻找解决方案,以每100速率1显示div随机而不创建其他div
  9. 如何判断字符串是一个字符串化的JSON对象

随机推荐

  1. TextView的众多使用
  2. android camera照片旋转90度
  3. android and arm
  4. android中自定义进度条风格
  5. Android单元测试和日志输出
  6. Android的所有权限说明
  7. 分析Android(安卓)根文件系统启动过程(ini
  8. android设置view透明度的效果
  9. 【Android】Scrollview 相关问题汇总
  10. eclipse中运行android工程启动失败的问题