阐明在编码中执行SP [英] Clarification on Exceuting SP in coding

查看:54
本文介绍了阐明在编码中执行SP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我们如何通过编码执行存储过程.

我用过两种方法.

1.通过像在sqlserver中一样直接执行

例如:

Hi All,

How can we executing Stored procedure from coding.

I have used two ways .

1. By directly executing like in sqlserver

eg:

exec spname '+ txt1.Text+','+ txt2.Text+'


2.通过使用sql参数

例如:


2. by using sql parameters

eg:

SqlParameter[] param = new SqlParameter[1];
        param[0] = new SqlParameter("@value1", txt1.Text);
        param[1] = new SqlParameter("@value2", txt2.Text);
        DataTable objDataTable = new DataTable();
        SqlConnection objConnection = new SqlConnection(sConnString);
            objConnection.Open();
            SqlCommand objCommand = new SqlCommand("spname", objConnection) { CommandType = CommandType.StoredProcedure };
            objCommand.Parameters.AddRange(param);
            objCommand.CommandTimeout = 0;
            SqlDataAdapter objDataAdapter = new SqlDataAdapter(objCommand);
            objDataAdapter.Fill(objDataTable);





哪个是最好的方法?它有任何性能问题吗?为什么?



问候,
Pal.





which is the best way ? is it have any performance issue and why?



Regards,
Pal.

推荐答案

它们不可比.
第一种方法只能在SQL Server上执行,第二种方法只能在您的应用程序主机pc上执行.除非两者是同一台机器,否则性能比较是不相关的.

第二个示例实际上使第一个示例无论如何都可以在SQL Server上执行.
They aren''t comparable.
The first way can only be executed on the SQL server, the second can only be executed on your application host pc. Unless the two are the same machine, the performance comparison is irrelevant.

And the second example effectively causes the first to be executed on the SQL server anyway.


这篇关于阐明在编码中执行SP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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