• C# DataGridView 与 DataSet


    以前得到DataSet的时候都是利用直接写sql语句(适合初学者)

    public static DataSet GetDs(string sqlstr)
        {
            ds = new DataSet();
            sqlconn = new SqlConnection();
            sqlconn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["connstr"];
            sqlconn.Open();
            SqlDataAdapter da = new SqlDataAdapter(sqlstr, sqlconn);
            da.Fill(ds);
            ConnClose();
            return ds;
        }

    使用存储过程

    SqlConnection sqlConn = new SqlConnection();
    sqlConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings["connstr"];
    sqlConn.Open();
    SqlDataAdapter sqlDa = new SqlDataAdapter("GetAuthListByRole", sqlConn);
    sqlDa.SelectCommand.CommandType = CommandType.StoredProcedure;
    sqlDa.SelectCommand.Parameters.Add(new SqlParameter("@rolename", SqlDbType.VarChar, 50));
    sqlDa.SelectCommand.Parameters[0].Value = roleName;
    sqlDa.Fill(ds);
    sqlConn.Close();
    return ds;

    DataTable获取存储过程的返回值

    将定存储过程为如下:

    CREATE PROCEDURE SP_TEST1
    AS
    SELECT * FROM TABLE_TEST;

    调用改存储过程

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = “NameofStoreProcedure”;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = conn;
    cmd.ExecuteNonQuery();

    联系SqlDataAdapter与DataTable

    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(yourdatatable);

    接着修改DataGridView空间在绑定前

    DataGridView dgv = new DataGridView();
    dgv.Columns.Add(“EnglishName”, “ChineseName”);
    dgv.Columns[“EnglishName”].DataProductName = “IDinDB”;
    dgv.DataSource = dt;

  • 相关阅读:
    linux命令(8):du命令
    linux命令(7):ipcs/ipcrm命令
    linux命令(6):tar命令
    linux命令(5):netstat命令
    linux命令(4):vmstat命令
    linux命令(3):rpm命令
    linux命令(2):grep命令
    linux命令(1):sed命令
    链表中倒数第k个节点
    链表刷题总结
  • 原文地址:https://www.cnblogs.com/rogerroddick/p/2983157.html
Copyright © 2020-2023  润新知