码迷,mamicode.com
首页 > 数据库 > 详细

C#连接数据库

时间:2018-11-21 15:39:09      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:for   .text   open   bsp   except   on()   odata   ram   ext   

 

连接数据库:

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(" 提醒有误!\n" + 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("查询失败!\n" + ex.Message);

}

}

 

C#连接数据库

标签:for   .text   open   bsp   except   on()   odata   ram   ext   

原文地址:https://www.cnblogs.com/FuLiuWei/p/SQLData.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!