English 中文(简体)
C#代码中的错误
原标题:Error in C# code
  • 时间:2011-11-11 09:01:29
  •  标签:
  • c#
  • sql
public void ExecStoredProc(string strprocName, SqlConnection sqlConnect, List<string> Paramvalues)
{
    if (ConnectToDB() == true)
    {
        SqlCommand cmd = new SqlCommand(strprocName, sqlConnect);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = strprocName;

        SqlParameter parameters = new SqlParameter();
        parameters.Value = Paramvalues;
        cmd.ExecuteNonQuery();
    }
}

当我通过程序名称(login时,该代码有两种参数,即user/code>和pwd。 虽然我利用这份清单增加参数,但并非通过这份清单。 <代码>cmd.ExecuteNon Query()中显示错误

最佳回答

虽然你宣布了一个新的SqlPara公墓,但你实际上并没有把该物体同厘米联系起来。 添加以下内容有助于:

cmd.Parameters.Add(parameters)
问题回答

You have to add sqlparamerter into cmd.Parameters cmd.Parameters(new SqlParameter("Uid",value)); and no need to write CommandText.

cmd.Parameters.Add( new SqlParameter { ... }).Value = Paramvalues;

除了你不再将参数重新添加到指挥之外,你还有一份显示仪表作为你的参数值。 你的点名使我相信,名单上的每一项目都是一个参数。 你们的法典目前是这样,你只是重新设定一个参数(没有名称)。

你们想要这样的东西(除非我们不知道参数名称):

foreach(string value in ParamValues)
{
    cmd.Parameters.AddWithValue(/*ParamName*/, value);
}

如果情况并非如此,而且你希望把扼杀清单作为单一参数的价值,那么这个问题就更加复杂,需要加以澄清。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Model = ObjectLibrary.Model;
using System.Configuration;
using System.Collections;
using System.Data;


public class Base
{
    protected ArrayList Parameter = new ArrayList();


    public DataTable FetchData(string spName)
    {
       //if you want to get it form app config else just hard code here
       string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
       return ConnectToSql(connectionString, spName);
    }
    protected void ClearParameter()
    {
        Parameter.Clear();
    }
    protected void AddParameter(string parameterName, object value)
    {
       Parameter.Add(new SqlParameter(parameterName,value));
    }

    private DataTable ConnectToSql(string connection, string spName)
    {
        System.Data.SqlClient.SqlConnection conn;
        conn = new System.Data.SqlClient.SqlConnection();
        conn.ConnectionString = connection;
        try
        {
            conn.Open();
            SqlCommand sqlCommand = new SqlCommand(spName, conn);
            sqlCommand.CommandType = CommandType.StoredProcedure;
            sqlCommand.Parameters.AddRange(Parameter.ToArray());
            SqlDataAdapter dataAdaptor = new SqlDataAdapter(sqlCommand);
            DataTable dt = new DataTable();
            dataAdaptor.Fill(dt);
            conn.Close();
            return dt;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
    }

  }

您可以使用这一类别。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签