How to use a DataAdapter with stored procedure and parameter

前端 未结 9 732
名媛妹妹
名媛妹妹 2020-11-30 01:05

I want to fill a DataGridView control using DataAdapter. But I don\'t know how to do it since I\'m using a stored procedure with parameter. Can someone cite an example pleas

相关标签:
9条回答
  • 2020-11-30 01:34
     SqlConnection con = new SqlConnection(@"Some Connection String");
     SqlDataAdapter da = new SqlDataAdapter("ParaEmp_Select",con);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                da.SelectCommand.Parameters.Add("@Contactid", SqlDbType.Int).Value = 123;
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
    
    0 讨论(0)
  • 2020-11-30 01:35

    Short and sweet...

    DataTable dataTable = new DataTable();
    try
    {
       using (var adapter = new SqlDataAdapter("StoredProcedureName", ConnectionString))
       {
           adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
           adapter.SelectCommand.Parameters.Add("@ParameterName", SqlDbType.Int).Value = 123;
           adapter.Fill(dataTable);
       };
    }
    catch (Exception ex)
    {
        Logger.Error("Error occured while fetching records from SQL server", ex);
    }
    
    0 讨论(0)
  • 2020-11-30 01:36

    I got it!...hehe

    protected DataTable RetrieveEmployeeSubInfo(string employeeNo)
            {
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter da = new SqlDataAdapter();
                DataTable dt = new DataTable();
                try
                {
                    cmd = new SqlCommand("RETRIEVE_EMPLOYEE", pl.ConnOpen());
                    cmd.Parameters.Add(new SqlParameter("@EMPLOYEENO", employeeNo));
                    cmd.CommandType = CommandType.StoredProcedure;
                    da.SelectCommand = cmd;
                    da.Fill(dt);
                    dataGridView1.DataSource = dt;
                }
                catch (Exception x)
                {
                    MessageBox.Show(x.GetBaseException().ToString(), "Error",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    cmd.Dispose();
                    pl.MySQLConn.Close();
                }
                return dt;
            }
    
    0 讨论(0)
  • 2020-11-30 01:36

    Here we go,

    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con; //database connection
    cmd.CommandText = "WRITE_STORED_PROC_NAME"; //  Stored procedure name
    cmd.CommandType = CommandType.StoredProcedure; // set it to stored proc
    //add parameter if necessary
    cmd.Parameters.Add("@userId", SqlDbType.Int).Value = courseid;
    
    SqlDataAdapter adap = new SqlDataAdapter(cmd);
    adap.Fill(ds, "Course");
    return ds;
    
    0 讨论(0)
  • 2020-11-30 01:45
       public DataSet Myfunction(string Myparameter)
        {
            config.cmd.Connection = config.cnx;
            config.cmd.CommandText = "ProcName";
            config.cmd.CommandType = CommandType.StoredProcedure;
            config.cmd.Parameters.Add("parameter", SqlDbType.VarChar, 10);
            config.cmd.Parameters["parameter"].Value = Myparameter;
    
            config.dRadio = new SqlDataAdapter(config.cmd);
            config.dRadio.Fill(config.ds,"Table");
    
            return config.ds;
    
    
        }
    
    0 讨论(0)
  • 2020-11-30 01:50
    SqlConnection con = new SqlConnection(@"Some Connection String");//connection object
    SqlDataAdapter da = new SqlDataAdapter("ParaEmp_Select",con);//SqlDataAdapter class object
    da.SelectCommand.CommandType = CommandType.StoredProcedure; //command sype
    da.SelectCommand.Parameters.Add("@Contactid", SqlDbType.Int).Value = 123; //pass perametter
    DataTable dt = new DataTable();  //dataset class object
    da.Fill(dt); //call the stored producer
    
    0 讨论(0)
提交回复
热议问题