过程或函数insert_data指定了太多参数 [英] Procedure or function insert_data has too many arguments specified

查看:95
本文介绍了过程或函数insert_data指定了太多参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#中的SP有一点问题,我试图从数组中插入数据但是失败并且gime下一个错误过程或函数insert_data指定的参数太多了。



Hi, i have a little problem with a SP in C#, i trying to insert data from a array but fails and gime the next error "Procedure or function insert_data has too many arguments specified."

using System.IO;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Data;

namespace ReadHugeTxt
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader(@"C:\file2.txt");
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CAS"].ConnectionString);
            SqlCommand cmd = new SqlCommand("insert_data", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            string line, lastline = " ";

            while ((line = sr.ReadLine()) != null)
            {
                string[] L = Regex.Split(line, ",");
                conn.Open();
                if (line != lastline)
                {
                    cmd.Parameters.AddWithValue("@sm", line[0]);
                    cmd.Parameters.AddWithValue("@st", line[1]);
                    cmd.Parameters.AddWithValue("@est", line[3]);

                    cmd.ExecuteNonQuery();
                    lastline = line;
                }
                conn.Close();
            }
        }
    }
}





存储过程





Stored Procedure

create procedure insert_data 
@sm nvarchar(50),
@st nvarchar(50),
@est nvarchar(50)

as 

insert into report (sma,st,est)
values(@sm,@st,(upper(@est)))





谢谢



Thanks

推荐答案

试试这个(警告未经测试)

Try this (warning not tested)
cmd.Parameters.Add("@sm", SqlDbType.NVarChar);
cmd.Parameters.Add("@st", SqlDbType.NVarChar);
cmd.Parameters.Add("@est", SqlDbType.NVarChar);

while ((line = sr.ReadLine()) != null)
{
    string[] L = Regex.Split(line, ",");
    conn.Open();
    if (line != lastline)
    {
        cmd.Parameters["@sm"].Value = line[0];
        cmd.Parameters["@st"].Value = line[1];
        cmd.Parameters["@est"].Value = line[3];

        cmd.ExecuteNonQuery();
        lastline = line;
    }
    conn.Close();
}


这篇关于过程或函数insert_data指定了太多参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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