简体   繁体   English

使用C#在存储的SQL过程中输入

[英]Input on a stored sql procedure using C#

In sql I normally execute my procedure using 在SQL中,我通常使用

exec dbo.usp_FCS 'TIMV','serial'

And I tried something somewhat the same in c# but it seems I got this wrong 我在c#中尝试了一些相同的东西,但似乎我错了

   using (SqlConnection connection = new SqlConnection("Data Source=;Initial Catalog=;User ID=;Password="))
            {
                using (SqlCommand cmd = new SqlCommand("usp_FCS_GetUnitInfo_Takaya" + "'" + MachineName + " ','serial' " , connection))
                {
                        try
                    {

                            connection.Open();
                            SqlDataAdapter da = new SqlDataAdapter(cmd);
                    }

                        catch (SqlException ex)
                    {
                        label6.Visible = true;
                        label6.Text = string.Format("Failed to Access Database!\r\n\r\nError: {0}", ex.Message);
                        return;
                    }
                }
            }

My question is,how can I give those 2 inputs 'TIMV' and 'serial' of my stored procedure using c#? 我的问题是,如何使用c#为存储过程提供这两个输入'TIMV'和'serial'?

Edit: 编辑:

I tried something like this: 我尝试过这样的事情:

 using (SqlCommand cmd = new SqlCommand("usp_FCS_GetUnitInfo_Takaya"  , connection))
                {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@p1", SqlDbType.VarChar).Value = MachineName;
                        cmd.Parameters.Add("@p2", SqlDbType.VarChar).Value = "serial";
                        try
                    {    my code...

And it is still not working 而且仍然无法正常工作

You use the Parameters collection of the SqlCommand class to send parameters to a stored procedure. 您可以使用SqlCommand类的Parameters集合将参数发送到存储过程。

Suppose your parameter names are @p1 and @p2 (Please, for your sake, don't use names like this ever) - your c# code would look like this: 假设您的参数名称为@p1@p2 (请为您方便起见,请勿使用像这样的名称)-您的C#代码如下所示:

using (var cmd = new SqlCommand("usp_FCS_GetUnitInfo_Takaya", connection))
{
    cmd..CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@p1", SqlDbType.VarChar).Value = MachineName;
    cmd.Parameters.Add("@21", SqlDbType.VarChar).Value = "serial";

    try
    {
       // rest of your code goes here....

Note: use the SqlDbType value that fits the parameters data type. 注意:使用适合参数数据类型的SqlDbType值。

The most correct way to add a parameter to an SqlCommand is through the Add method that allows you to specify the datatype of the parameter and, in case of strings and decimals, the size and the precision of these values. 将参数添加到SqlCommand的最正确方法是通过Add方法,该方法允许您指定参数的数据类型,并在指定字符串和小数的情况下指定这些值的大小和精度。 In that way the Database Engine Optimizer can store your query for reuse and be a lot faster the second time you call it. 这样,数据库引擎优化器可以存储查询以供重用,并且在第二次调用它时会更快。 In your case I would write 你的情况我会写

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@mname", SqlDbType.NVarChar, 20).Value = MachineName;
cmd.Parameters.Add("@serial", SqlDbType.NVarChar, 20).Value = "serial";

This assumes that your stored procedure receives two parameters named EXACTLY @mname and @serial , the type of the parameters is NVarChar and the length expected is 20 char. 假设您的存储过程接收到两个名为EXACTLY @mname@serial的参数,这些参数的类型为NVarChar,预期长度为20个字符。 To give a more precise answer we need to see at least the first lines of the sp. 为了给出更精确的答案,我们至少需要查看sp的第一行。

In your code above also the execution of the command is missing. 在上面的代码中,命令的执行也丢失了。 Just creating the command does nothing until you execute it. 在执行命令之前,仅创建命令不会执行任何操作。 Given the presence of an SqlDataAdapter I think you want to fill a DataSet or a DataTable and use this object as DataSource of your grid. 考虑到SqlDataAdapter的存在,我认为您想填充DataSet或DataTable并将该对象用作网格的DataSource。 Something like this 像这样

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
yourDataGrid.DataSource = dt;

And if this is an ASP.NET app, also the DataBind call 如果这是一个ASP.NET应用程序,那么也会调用DataBind

yourDataGrid.DataBind();

Try this: 尝试这个:

   DataSet ds = new DataSet("dts");
        using (SqlConnection conn = new SqlConnection
          ("Data Source=;Initial  Catalog=;User ID=;Password="))
        {
            try
            {
    SqlCommand sqlComm = new  SqlCommand("usp_FCS_GetUnitInfo_Takaya",conn);
                sqlComm.Parameters.AddWithValue("@p1", MachineName);
                sqlComm.Parameters.AddWithValue("@p2", "serial");
                sqlComm.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = sqlComm;
                da.Fill(ds);
            }
            catch (Exception e)
            {
                label6.Visible = true;
                label6.Text = string.Format
              ("Failed to Access  Database!\r\n\r\nError: {0}", ex.Message);
                return;
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM