连接数据库:
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);
}
}