嗨我想從本地SQL服務數據庫獲取數據以從用戶註冊表單中獲取輸入。但是當我按下它沒有記錄到可服務數據庫的按鈕時。C#使用SQL登錄應用程序
我需要使用執行非查詢嗎?我將如何解決這個代碼?感謝
using System.Data.Sql;
using System.Data.SqlClient;
namespace Paddle_Power
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PaddlePower.mdf;Integrated Security=True;User Instance=True";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
MessageBox.Show("open");
}
catch (Exception)
{
MessageBox.Show("Did not connect");
}
string username = textBox1.Text;
string password = textBox2.Text;
string sqlquery = ("SELECT * FROM User WHERE Username = '" + textBox1.Text + "'");
sqlquery = "INSERT INTO [User] (Username, Password) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "')";
SqlCommand command = new SqlCommand(sqlquery, cn);
command.Parameters.AddWithValue("Username", username);
command.Parameters.AddWithValue("Password", password);
command.Parameters.Clear();
}
}
}
你爲什麼分配給sqlquery,然後立即重新分配?你爲什麼不執行SQL?你沒有正確使用參數...你有沒有試過一個教程? –
是的,我從一個教程得到了這個,但它有很多負面評論說同樣的事情。你能幫我重新寫這個嗎? – stefan
如果您不忽略拋出哪個異常,它可能也會幫助您排除故障。沒有try/catch塊,你會做得更好。 –