問題描述
我有一個簡單的創建一個新用戶表單,它從兩個文本框中獲取值,用戶名和密碼.button2 單擊事件應采用這些值并將它們插入到數據庫中的用戶表中.但是,當我運行我的代碼時,會出現消息框說數據已添加,我無法使用 VS2010 看到數據庫中的數據.
I have a simple create a new user form which takes values from two text boxes, username and password. The button2 click event should take these values and insert them into the Users table in the database. However, when I run my code the message box appears to say the data has been added, I cannot see the data in the database using VS2010.
查看 VS 中數據庫連接的屏幕截圖.我還在 VS 中創建了數據庫的數據源.
See screen shot for the database connection in VS. I have also created a datasource of the database in VS.
有什么想法嗎?
非常感謝.
private void button2_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
string sqlquery;
string connection = @"Data Source=.SQLEXPRESS;AttachDbFilename='C:UsersNickDocumentsVisual Studio 2010ProjectsDebenhamsProjectOffice V.01DebenhamsProjectOffice V.01DebenhamsProjectOfficeDatabase.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
}
catch (Exception)
{
MessageBox.Show("Unable to connect to Database");
}
sqlquery = "INSERT INTO Users (Username, Password) VALUES ('" + txtUsername.Text + "','" + txtPassword.Text + "')";
try
{
SqlCommand command = new SqlCommand(sqlquery, cn);
command.Parameters.AddWithValue("Username", username);
command.Parameters.AddWithValue("Password", password);
command.Parameters.Clear();
MessageBox.Show("User Added");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
txtUsername.Text = "";
txtPassword.Text = "";
cn.Close();
}
推薦答案
只是嘗試修復代碼.有些元素很重要,有些元素只是優雅.試試看,它可能會起作用.或者可以指出錯誤所在:
Just trying a code fix. Some elements are crucial, some are just elegant. Try it, it might work., or could point to where the error is:
private void button2_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
string sqlquery;
//Put away the apostrophes and used twice double quotations for
//the full path of the database file:
string connection = @"Data Source=.SQLEXPRESS;AttachDbFilename=""C:UsersNickDocumentsVisual Studio 2010ProjectsDebenhamsProjectOffice V.01DebenhamsProjectOffice V.01DebenhamsProjectOfficeDatabase.mdf"";Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection cn = new SqlConnection(connection);
/* Better to let the program fail than think it's open and moving on
removed try, catch*/
cn.Open();
//Why using your TextBoxes values if you already created strings?
//changed
//you should also be careful users can't type something like "') in the
//textboxes or they may cause a SQL injection
sqlquery = "INSERT INTO Users (Username, Password) VALUES ('" + username + "','" + password + "')";
try
{
SqlCommand command = new SqlCommand(sqlquery, cn);
/* unnecessary since you already built a query command.Parameters.AddWithValue("Username", username);
command.Parameters.AddWithValue("Password", password);
command.Parameters.Clear(); */
//Missing!!
command.ExecuteNonQuery();
MessageBox.Show("User Added");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
//Elegance
txtUsername.Clear();
txtPassword.Clear();
cn.Close();
}
這篇關于C# 表單未將值插入 SQL Server 數據庫的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!