• C#连接数据库


    连接数据库:

    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);

    }

    }

  • 相关阅读:
    疫情期间刷题列表
    P3379 【模板】最近公共祖先(LCA)
    P3865 【模板】ST 表
    Distance Queries
    Frequent values
    P3388 【模板】割点(割顶)
    P1168 中位数
    单链表排序
    大数据各组件重要技术点总结
    Flink CDC同步MySQL分库分表数据到Iceberg数据湖实践
  • 原文地址:https://www.cnblogs.com/FuLiuWei/p/SQLData.html
Copyright © 2020-2023  润新知