如何使用ADO.NET将值发送到不带ParameterName的SP [英] How to send values to SP without ParameterName using ADO.NET

查看:53
本文介绍了如何使用ADO.NET将值发送到不带ParameterName的SP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,你好吗,我希望一切顺利:)

Hi All ,How Are You , i hope doing well :)

SqlConnection mySqlConnection = new SqlConnection(ConnectionString);
            SqlCommand mySqlCommand = new SqlCommand("InsertData", mySqlConnection);
            mySqlCommand.CommandType = CommandType.StoredProcedure;
            mySqlCommand.Parameters.Add("@UsrID", SqlDbType.NVarChar).Value = "ID0001";
            mySqlCommand.Parameters.Add("@UsrFName", SqlDbType.NVarChar).Value = "FirstName"; 
            mySqlCommand.Parameters.Add("@UsrLName", SqlDbType.NVarChar).Value = "LastName";



我可以在不放置参数名称(@ParameterName)和参数类型(SqlDbType.ParameterType)的情况下使用此代码吗?
只是,我想将值发送到存储过程


我的问候,



Can i using this code without put Parameter Name (@ParameterName) and Parameter Type (SqlDbType.ParameterType)
just , i want to send the values to Stored Procedure


my greetings ,

推荐答案

要将参数发送到存储过程,您必须给两个信息位:参数名称和参数值.如果错过了名称,则SP将不知道将值放在哪里,如果错过了值,则SP将不知道要输入什么!

您可以选择:按示例中的方式使用Parameters.Add,或改用Parameters.AddWithValue:
In order to send the parameters to a stored procedure, you have to give two bits of information: the parameter name, and the parameter value. If you miss out the name, the SP does not know where to put the value, and if you miss out the value, the SP does not know what to put in!

You have a choice: Use Parameters.Add as you are in your example, or use Parameters.AddWithValue instead:
SqlConnection mySqlConnection = new SqlConnection(ConnectionString);
SqlCommand mySqlCommand = new SqlCommand("InsertData", mySqlConnection);
mySqlCommand.CommandType = CommandType.StoredProcedure;
mySqlCommand.Parameters.AddWithValue("@UsrID", "ID0001");
mySqlCommand.Parameters.AddWithValue("@UsrFName", "FirstName");
mySqlCommand.Parameters.AddWithValue("@UsrLName", "LastName");


您是否通过这种方式尝试过:

Have you tried by this way :

SqlConnection mySqlConnection = new SqlConnection(ConnectionString);
            SqlCommand mySqlCommand = new SqlCommand("exec InsertData ID0001 , FirstName, LastName", mySqlConnection);
            mySqlCommand.CommandType = CommandType.Text;



更改代码,以便它动态生成命令字符串.

但是,您问题中的方法是执行此操作的标准方法.

另外,请看此页面:
http://msdn.microsoft.com/en-us/library/fksx3b4f%​​28v = vs.80%29.aspx [ ^ ]



Change the code so that it build the command string dynamically.

However the method in your question is the standard way to do this.

Also, have a look at this page :
http://msdn.microsoft.com/en-us/library/fksx3b4f%28v=vs.80%29.aspx[^]


这篇关于如何使用ADO.NET将值发送到不带ParameterName的SP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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