我想做一個簡單的程序,它有一個本地數據庫僅用於testing.And登錄的一部分,當我嘗試打開連接到SQL數據庫時,我不斷收到錯誤。登錄表單,SQL異常
private void logInButton_Click(object sender, EventArgs e)
{
MainMenu openMainMenu = new MainMenu();
SqlConnection sqlcon = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C: \Users\Nea Florin\Desktop\PlatformaTestare\PlatformaTestare\Server.mdf;Integrated Security=True;Connect Timeout=30");
sqlcon.Open();
SqlCommand cmd = new SqlCommand("Select * from Table Where username ='" + usernameTextBox.Text + "' and password = '" + passwrodTextBox.Text + "'");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dtbl = new DataTable();
sda.Fill(dtbl);
if (dtbl.Rows.Count > 0)
{
openMainMenu.Show();
this.Hide();
}
else
MessageBox.Show("Wrong username or password!");
}
我得到sqlcon.Open()的錯誤,和它是:「類型的未處理的異常‘System.Data.SqlClient.SqlException’出現在system.data.dll
附加信息:嘗試爲文件C:\ Users \ Nea Florin \ Desktop \ PlatformaTestare \ PlatformaTestare \ Server.mdf附加一個自動命名的數據庫失敗。具有相同名稱的數據庫存在,或者指定的文件無法打開,或者它是位於UNC份額。「
的文件名,你有C的'空格:'和'\用戶\ .... mdf' ... – t0mm13b
之前,你做任何事情,閱讀關於sql注入和參數化查詢。之後,閱讀密碼散列。 –
另外,瞭解異常情況以及如何正常處理它。 – t0mm13b