gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:58 24 4
gpt4 key购买 nike

我有一个持续错误的问题

“寻找过程或函数‘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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com