c# - 使用 C# 和存储过程解析文本文件并将其加载到数据库中

标签 c# tsql load

我有一个持续错误的问题

“寻找过程或函数‘Cup_INSFuneralHome’需要参数‘@funeralhomename’,但未提供。”

我做了很多修改,但没有任何效果,所以我不得不从我在错误的区域寻找。

当我调试时,它似乎以正确的顺序提取了正确的信息。原始文本文件格式为:

公司名称
地址
城邦 zip
电话

我很纠结于此。我浏览了其他帖子,但未能找到我需要的内容,或者无法从众多“有点”相似的帖子中提取我需要的内容。恐怕问题就在我面前,但我就是看不到它。我需要一双全新的眼睛。所以我提前道歉。

过程代码:

ALTER procedure [dbo].[Cup_INSFuneralHome]
@funeralhomename nvarchar(50),
@addressone nvarchar(50),
@addresstwo nvarchar(50),
@CityName nvarchar(50),
@State nvarchar(10),
@Zipcode nchar(10),
@Telephone nchar(15),
@PrimaryContact nvarchar (50),
@EmailAddress nvarchar (50)
as

declare @cityid uniqueidentifier
declare @stateid uniqueidentifier 

select @cityid = (select cityid from [city] where CityName=(@CityName)) 
select @stateid = (select stateid from [State] where StateCode=ltrim(rtrim(@State)))

insert funeralhome(funeralhomename, addressone, addresstwo, cityid, stateid, zipcode,  telephone, PrimaryContact, EmailAddress)
values (@funeralhomename, @addressone, @addresstwo, @CityName, @State, @ZipCode,     @Telephone, @PrimaryContact, @EmailAddress)

C#代码:

namespace abc
{
class Program
{
    static void Main(string[] args)
    {
        address myclass = new address();
        string dbConnection = "xyz";
        string path = "D:\\docs\\someDocument.txt";
        StreamReader sr = new StreamReader(path);

        int intcount=0;
        string sLine = "";
        ArrayList fhome = new ArrayList();
        ArrayList Ads = new ArrayList();

        while (sLine != null)
        {
            sLine = sr.ReadLine();
            if (sLine != null)
                fhome.Add(sLine);
            intcount=intcount+1;
        }

        sr.Close();

        int startcount=0;

        for (int n = 0; n < fhome.Count; n++)
        {
            char[] delim = {',', ' '};

            try
            {
                if (startcount == 0)
                {
                    myclass = new address();
                }

                if (fhome[n].ToString().Trim().Length > 0)
                {
                    if (!fhome[n].ToString().Contains("Funeral Home Profile"))
                    {
                        switch (startcount)
                        {
                            case 0:
                                myclass.company = fhome[n].ToString().Trim();
                                startcount = startcount + 1;
                                break;

                            case 1:
                                myclass.address1 = fhome[n].ToString().Trim();
                                startcount = startcount + 1;
                                break;

                            case 2:
                                myclass.address2 = fhome[n].ToString().Trim();
                                startcount = startcount + 1;
                                break;

                            case 3:
                                myclass.telephone = fhome[n].ToString().Trim();
                                startcount = 0;
                                Ads.Add(myclass);
                                break;
                        }
                    }
                }
            }               
            catch { }
        }

       SqlConnection conn = new SqlConnection(dbConnection);

       for(int n=0;n< Ads.Count;n++)
        {           
           address tclass = (address)Ads[n];

           int comloc;
           comloc = tclass.address2.IndexOf(",");             

              string funeralhomename = tclass.company.ToString();
              string street = tclass.address1.ToString();
              string street2 = "";
              string city = tclass.address2.Substring(0, comloc);
              string state = tclass.address2.Substring(comloc + 1, 3);
              string zip = tclass.address2.Substring(comloc + 4);
              string tel = tclass.telephone.Replace("Local:", "");
              string PrimaryContact = "";
              string EmailAddress = "";

              string tsql = ""; 

               tsql = (funeralhomename + ',' +
                      street + ',' + street2 + city + ',' +
                      state + zip + tel + PrimaryContact + EmailAddress);

           conn.Open();

           try
           {
               SqlCommand cmd = new SqlCommand("Cup_INSFuneralHome", conn);
               cmd.CommandType = CommandType.StoredProcedure;

               SqlParameter[] param = new SqlParameter[9];
               param[0] = new SqlParameter("@funeralhomename", SqlDbType.NVarChar);
               param[0].Value = funeralhomename;
               param[1] = new SqlParameter("@addressone", SqlDbType.NVarChar);
               param[1].Value = street;
               param[2] = new SqlParameter("@addresstwo", SqlDbType.NVarChar);
               param[2].Value = street2;
               param[3] = new SqlParameter("@cityname", SqlDbType.NVarChar);
               param[3].Value = city;
               param[4] = new SqlParameter("@State", SqlDbType.NVarChar);
               param[4].Value = state;
               param[5] = new SqlParameter("@zipCode", SqlDbType.NChar);
               param[5].Value = zip;
               param[6] = new SqlParameter("@Telephone", SqlDbType.NChar);
               param[6].Value = tel;
               param[7] = new SqlParameter("@PrimaryContact", SqlDbType.NVarChar);
               param[7].Value = PrimaryContact;
               param[8] = new SqlParameter("@EmailAddress", SqlDbType.NVarChar);
               param[8].Value = EmailAddress;

               cmd.ExecuteNonQuery();
           }

           catch
           {
               Debug.Print("Error with.." + tclass.company);
           }

           finally
           {
               conn.Close();
           }
           Debug.Print(tsql);
        }   
    }        
}
}
public class address 
{
private string _company;
private string _address1;
private string _address2;
private string _telephone;

public string company
{
    get { return _company; }
    set { _company = value; }
}

public string address1
{
    get { return _address1; }
    set { _address1 = value; }
}

public string address2
{
    get { return _address2; }
    set { _address2 = value; }
}

public string telephone
{
    get { return _telephone; }
    set { _telephone = value; }
}
}

最佳答案

您没有将 sql 参数添加到 cmd 对象;-)

关于c# - 使用 C# 和存储过程解析文本文件并将其加载到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4451242/

相关文章:

C# 编码回调

c# - 找不到蓝牙设备的 ID

c# - 异步任务正在卡住 UI

sql - 范围身份与当前身份

C# 和 SQL 服务器 : execute stored procedure

基于 URL 模式的 Apache HTTP 负载均衡

jquery - 使用 .click 切换 jQuery .load

c# - LINQ vs foreach vs for性能测试结果

SQL 按日期获取前 10 条记录

javascript - 如何在浏览页面时保持音频播放?