我正在创建一个数据库,以便可以存储用户名和密码。我已经在 Microsft Visual Express C# 2010 上成功创建了一个数据库。
我的程序似乎运行良好,并且没有产生错误消息,除了我没有收到任何反馈:看起来我的程序实际上并没有向数据库发出请求,或者没有返回结果。这是怎么回事?
private void button1_Click(object sender, EventArgs e)
{
string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
}
catch (Exception)
{
MessageBox.Show("Did not connect");
}
string username = textBox1.Text;
string password = maskedTextBox1.Text;
string sqlquery = ("SELECT * FROM User WHERE Username = '" + textBox1.Text + "'");
sqlquery = "INSERT INFO [User] (Username, Password) VALUES ('" + textBox1.Text + "','" + maskedTextBox1.Text + "')";
SqlCommand Command = new SqlCommand(sqlquery, cn);
Command.Parameters.AddWithValue("Username", username);
Command.Parameters.AddWithValue("Password", password);
}
private void Form1_load(object sender, EventArgs e)
{
string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
}
catch (Exception)
{
MessageBox.Show("Did not connect");
}
}
}
}