How to use a DataAdapter with stored procedure and parameter

前端 未结 9 781
名媛妹妹
名媛妹妹 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: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;
    

提交回复
热议问题