Criei um banco, mas não consigo fazer conexão com ele pelo C#, não sei dizer se é algum problema com o usuário/senha que estou utilizando ou algo do gênero...
botão para testar:
using System.Data.SqlClient;
private void buttonSqlServer_Click(object sender, EventArgs e)
{
string connectionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
string server = textServidor.Text;
string banco = textBanco.Text;
string usuario = textUsuario.Text;
string senha = textSenha.Text;
connectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}",server,banco,usuario,senha);
MessageBox.Show(connectionString);
connection = new SqlConnection(connectionString);
try
{
connection.Open();
sql = "select * from tabela";
command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
MessageBox.Show(" ExecuteNonQuery in SqlCommand executed !!");
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection !\n" + ex.ToString());
}
}
---------------------------
Data Source=PROG_SAMIR\SQLEXPRESS;Initial Catalog=controle_compras;User ID=teste_user;Password=123456
---------------------------
OK
---------------------------
Exception gerado:
---------------------------
---------------------------
Can not open connection !
System.Data.SqlClient.SqlException (0x80131904): Falha de logon do usuário 'teste_user'.
em System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling)
em System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
em System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
em System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
em System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
em System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
em System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
em System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
em System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
em System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
em System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
em System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
em System.Data.SqlClient.SqlConnection.Open()
em TesteSQL.FormPrincipal.buttonSqlServer_Click(Object sender, EventArgs e) na C:\Users\Samir\documents\visual studio 2015\Projects\TesteSQL\TesteSQL\Form1.cs:linha 137
ClientConnectionId:e76ca719-af59-4823-86b4-713629ee744c
Error Number:18456,State:1,Class:14
---------------------------
OK
---------------------------