簡體   English   中英

System.Data.SqlClient.SqlException的奇怪情況

[英]Strange situation with System.Data.SqlClient.SqlException

您好,我正在嘗試使用C#連接到數據庫,但一直將此代碼提供給我:

SqlConnection connection = new SqlConnection("Data Source=FAVAI_LAB_01\\SQLSERVER;Initial Catalog=DBServicios;Integrated Security=True");

try
{
   connection.Open();

   SqlCommand myCommand = new SqlCommand("select ID_USUARIO from TUSUARIO" +
                                         "WHERE NOMBRE_USUARIO=@Param1 AND PASS=@Param2", connection);
   myCommand.Parameters.AddWithValue("@Param1", username);
   myCommand.Parameters.AddWithValue("@Param1", password);

   string CurrentName = (string)myCommand.ExecuteScalar();

   if (CurrentName != null)
   {
      connection.Close();
      Console.WriteLine("true");
   }
   else
   {
      connection.Close();
      Console.WriteLine("falsfgdge");
   }
}
catch (System.Data.SqlClient.SqlException e)
{
    Console.WriteLine(e.ToString());
}

這是錯誤:

System.Data.SqlClient.SqlException (0x80131904):
 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)
 at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.
 Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, 
 Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, 
 Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) at 
 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword,
 Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, 
 SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, 
 Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, 
 DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, 
 DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) 
 at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) 
 at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, 
 DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() 
 at Webserver.Login.getAccess(String username, String password) in path\Login.cs:line 18

TUSUARIO和WHERE之間缺少空格,請添加一個

 SqlCommand myCommand = new SqlCommand("select ID_USUARIO from TUSUARIO " +
                            "WHERE NOMBRE_USUARIO=@Param1 AND PASS=@Param2", connection);

您正在選擇ID_USUARIO,但隨后將其轉換為字符串。 最有可能的是,如果列稱為ID_something,則其類型不是字符串類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM