private void Btn_login_Click(object sender, EventArgs e)
{
//SqlConnection cnn = new SqlConnection(); //使用Data介面SqlClient類中成員SqlConnection
//cnn.ConnectionString = "Server=127.0.0.1;UID=sa;PWD=admin;DataBase=factory"; //給連線物件字串
string connStr = "Server=127.0.0.1;UID=sa;PWD=admin;DataBase=factory";
string cmdStr = "select user_name from users where user_name=@user_name and user_ps=@user_ps ";
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new System.Data.SqlClient.SqlCommand(cmdStr);
SqlParameter[] parameters = {
new SqlParameter("@user_name", SqlDbType.NVarChar,50),
new SqlParameter("@user_ps", SqlDbType.NVarChar,50)
};
parameters[0].Value = tb_user_name.Text.ToString();
parameters[1].Value = tb_user_ps.Text.ToString();
try
{
cmd.Connection = conn;
foreach (var item in parameters)
{
cmd.Parameters.Add(item);
}
cmd.Connection.Open();
SqlDataReader thisReader =cmd.ExecuteReader();
while(thisReader.Read())
{
this.Hide();
Form2 form2 = new Form2();
// form2.WindowState = FormWindowState.Maximized;
form2.Show();
}
}
catch (Exception err)
{
Console.WriteLine("讀取資料庫出錯" + err.Message);
}
finally
{
//MessageBox.Show("讀取資料庫成功", "成功");
conn.Close();
}
}