C#中使用SQL SERVER2000的存储过程

string strConn = "Initial Catalog=Brest_Student;" +
                              "Data Source=10.51.160.18;" +
                              "Integrated Security=SSPI;" +
                              "User ID=sa;Password=643jkfd8LFM";

// 可用这种方法在面向连接的环境中使用存储过程
string sql = "exec p_Student @id='0408021202'";

SqlConnection mycn = new SqlConnection(strConn);
SqlCommand mycmd = new SqlCommand(sql,mycn);
mycn.Open();

try
{
    mycmd.ExecuteNonQuery();
    SqlDataReader sdr = mycmd.ExecuteReader();
    if(sdr.Read())
    {
        // 得到返回的表中的姓名字段
        textBox1.Text = sdr["姓名"].ToString();
    }
}
catch(Exception ex)
{
    MessageBox.Show("错误");
}
finally
{
    mycmd.Dispose();
    mycn.Close();
}

数据库中字义了如下存储过程
CREATE PROCEDURE p_Student
    @id VARCHAR(15)
AS
    SELECT * FROM 学生 WHERE 学号=@id

posted on 2006-01-19 21:42 samuel's blog 阅读(154) 评论(0)  编辑 收藏

公告

昵称:samuel's blog
园龄:6年
粉丝:0
关注:0

导航

统计

  • 随笔 - 21
  • 文章 - 10
  • 评论 - 62
  • 引用 - 0

搜索

 
 

常用链接

随笔分类

随笔档案

文章分类

相册

连接

最新评论

阅读排行榜

推荐排行榜