連線資料庫:
try
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Data Source = (local); Initial Catalog =manage; Integrated Security = SSPI;”;
conn.Open();
if (conn.State == ConnectionState.Open)
{
string sqlstr =
“select * from 管理員登陸表 where id=@id”;
SqlCommand cmd = new SqlCommand(sqlstr, conn);
cmd.Parameters.AddWithValue(“id”, textBox1.Text);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string ss=dr[“pass”].ToString();
int i;
for ( i = 0; i < textBox2.Text.Length; i++)
if (ss[i] == textBox2.Text[i])
{
continue;
}
else break;
if (i==textBox2.Text.Length )
s = “s”;
}
}
conn.Close();
}
catch (Exception ee)
{
MessageBox.Show(” 查詢出錯! ” + ee.Message);
}
finally {
if (s == “s”)
{
user ur = new user(Convert.ToInt32(textBox1.Text), textBox2.Text);
Program.u = ur;
Program.sigin = true;
this.Close();
}
else
{
MessageBox.Show(” 無此賬號或密碼錯誤! “);
}
}
把查詢的資訊新增到dataview中:
try{
string s=””;
SqlConnection conn = new SqlConnection();
conn.ConnectionString = “Data Source = (local); Initial Catalog =manage; Integrated Security = SSPI;”;
conn.Open();
if (conn.State == ConnectionState.Open)
{
string sqlstr = “Select * from 我的訂單 “;//where 計劃交貨>”+DateTime.Now;
SqlCommand cmd = new SqlCommand(sqlstr, conn);
try
{
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
s=””;
s=dr[“製作單號”].ToString()+”,”;
s+=dr[“盒號”].ToString()+”,”;
s+=dr[“是否返工”].ToString()+”,”;
s+=dr[“公司名稱”].ToString()+”,”;
s+=dr[“地區”].ToString()+”,”;
s+=dr[“主管姓名”].ToString()+”,”;
s+=dr[“客戶單號”].ToString()+”,”;
s+=dr[“計劃交貨”].ToString();
addTodataview(s);
}
}
catch (Exception mye)
{
MessageBox.Show(” 提醒有誤!
” + mye.Message);
}
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(“儲存連線失敗” + ex.Message);
}
private void addTodataview(string s)
{
try
{
string[] sp = s.Split(`,`);
dataGridView1.ClearSelection();
dataGridView1.Rows.Add(sp);
}
catch (Exception ex)
{
MessageBox.Show(“查詢失敗!
” + ex.Message);
}
}