简单C# 与SQL存储过程调用

存储过程

 

create proc test
@lc_code varchar(50)
as
select lc_name from  lc_location where lc_code=@lc_code

DataSet

SqlConnection conn = new SqlConnection("Server=dserver;database=lawmanfepos;uid=sa;pwd=");
            SqlDataAdapter sar = new SqlDataAdapter("test", conn);
            sar.SelectCommand.CommandType = CommandType.StoredProcedure;
            sar.SelectCommand.Parameters.Add("@lc_code", SqlDbType.VarChar, 50);
            sar.SelectCommand.Parameters["@lc_code"].Value = "001";

            DataSet ds = new DataSet();
            sar.Fill(ds, "aa");

            DataTable dt = ds.Tables[0];
            dataGridView1.DataSource = dt;

DataReader

SqlConnection conn = new SqlConnection("Server=dserver;database=lawmanfepos;uid=sa;pwd=");

            SqlCommand cmd = new SqlCommand("test", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@lc_code", SqlDbType.VarChar, 50);
            cmd.Parameters["@lc_code"].Value = "001";

            conn.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            while (sdr.Read())
            {
                MessageBox.Show(sdr["lc_name"].ToString());
            }
 

posted @ 2007-07-13 16:33  杰仔  阅读(519)  评论(0编辑  收藏  举报