C# Can';t连接到SQL Server-内部连接致命错误
我刚开始使用C#和Windows窗体,所以如果你看到一个很大的安全漏洞,请不要对我大喊大叫(我想知道)。每次运行此命令时,都会出现C# Can';t连接到SQL Server-内部连接致命错误,c#,sql-server,winforms,C#,Sql Server,Winforms,我刚开始使用C#和Windows窗体,所以如果你看到一个很大的安全漏洞,请不要对我大喊大叫(我想知道)。每次运行此命令时,都会出现内部连接致命错误,堆栈跟踪如下: System.InvalidOperationException: Internal connection fatal error. at System.Data.SqlClient.TdsParserStateObject.TryProcessHeader() at System.Data.SqlClient.TdsPa
内部连接致命错误
,堆栈跟踪如下:
System.InvalidOperationException: Internal connection fatal error.
at System.Data.SqlClient.TdsParserStateObject.TryProcessHeader()
at System.Data.SqlClient.TdsParserStateObject.TryPrepareBuffer()
at System.Data.SqlClient.TdsParserStateObject.TryReadByteArray(Byte[] buff, Int32 offset, Int32 len, Int32& totalRead)
at System.Data.SqlClient.TdsParser.ConsumePreLoginHandshake(Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean& marsCapable)
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, SessionData reconnectSessionData)
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, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
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 oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at Program.Login.btnLogin_Click(Object sender, EventArgs e) in i:\Users\Me\Documents\visual studio 2013\Projects\Program\Program\Login.cs:line 57
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
下面是它的代码:
//Begin SQL connection
using (SqlConnection conn = new SqlConnection("Data Source=tcp:**.**.**.**,3306;Initial Catalog=******;User ID=*****;Password=*****"))
{
conn.Open();
string UserName = txtUsername.Text;
string Password = txtPassword.Text;
//Hash password
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] buffer = encoder.GetBytes(Password);
SHA1CryptoServiceProvider cryptoTransformSHA1 =
new SHA1CryptoServiceProvider();
string hash = BitConverter.ToString(
cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "");
SqlCommand cmd = new SqlCommand("SELECT * FROM `users` WHERE `login` = @username AND password = @password", conn);
cmd.Parameters.AddWithValue("username", UserName);
cmd.Parameters.AddWithValue("password", hash);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
System.Data.SqlClient.SqlDataReader dr = null;
dr = cmd.ExecuteReader();
conn.Close();
if (dr.Read())
{
if (UserName == dr["username"].ToString() && hash == dr["password"].ToString())
{
Form1 f = new Form1();
f.Show();
this.Hide();
}
else
{
errorProvider1.SetError(txtUsername, "Your username and/or password are incorrect.");
}
}
}
尝试使用1433端口,它是用于MSSQL的,而不是3306端口,据我所知它是用于MySQL的。如果您使用的是MySQL,那么您需要使用
MySQL.Data.MySqlClient.MySqlConnection
而不是System.Data.SqlClient.SqlConnection
MySqlConnection
用于MySql数据库SqlConnection
用于MSSql数据库。不要混淆两者。在哪一行出现错误?@DavidG调试器在conn.Open()上给我一个InvalidOperationException未处理的错误代码>行。我之前在配置SQL时遇到了一些问题,我认为通过使用tcp和添加端口可以使SQL正常工作。请查看以了解有效的SQL Server连接字符串的外观。你的闻起来像MySQL,不是SQLServer@marc_s是的,它是MySQL。我来自PHP,SQL是微风。SQL Server也是微风-如果你做得正确的话:-)我使用MySQL,而不是MSSQL。