调用存储过程 [英] To Call a stored Procedure

查看:60
本文介绍了调用存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
在C#WinApp中,我将调用存储过程并在listView中显示结果.我使用datareader.

非常感谢...

Hello everybody,
in a C# WinApp I would to call a stored procedure and to display a results in a listView.I use datareader.

Thanks so much...

推荐答案

请使用下面的代码

Hi, use below code

SqlCommand command = new SqlCommand();
        connection.Open();
command.Connection = connection;
command.CommandType=CommandType.StoredProcedure;
command.CommandText = "SPName";
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
                    reader.GetString(1));
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();


您可以使用它.
You can use this..

SqlCommand cmd=new SqlCommand("spMyProc", con);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader sdr=new SqlDataReader();
sdr=cmd.ExecuteReader();
while(sdr.read()){
    //Your code goes here.
}
sdr.Dispose();
cmd.Dispose();
con.Close();


SqlConnection conn = new SqlConnection("connectionString");
SqlCommand cmd =新的SqlCommand("storedProcedureName",conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader rd = cmd.ExecuteReader();
if(rd.HasRows)
{
while(rd.read()){
//您的逻辑停留在这里
}
}
SqlConnection conn = new SqlConnection("connectionString");
SqlCommand cmd = new SqlCommand("storedProcedureName", conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlDataReader rd = cmd.ExecuteReader();
if(rd.HasRows)
{
while(rd.read()){
//Your logic stays here
}
}


这篇关于调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆