0
請幫我解決這個問題。我有這個小的應用程序來加載txt文件到一個sql數據庫,它與sqlite工作正常。當我移植到SQL時,我開始獲取'參數已經聲明'的錯誤。如果任何人都可以幫助我重新組織這段代碼,那就太棒了!我需要獲得環之類的東西之外的參數定義..循環中的C#SQL參數錯誤
using System;
using System.Data;
using System.Data.SQLite;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
namespace JohnDeereCMMDataParser
{
internal class Program
{
public static List<string> GetImportedFileList()
{
List<string> ImportedFiles = new List<string>();
using (SqlConnection connect = new SqlConnection(@"Server=FRXSQLDEV;Database=RX_CMMData;Integrated Security=YES"))
{
connect.Open();
using (SqlCommand fmd = connect.CreateCommand())
{
fmd.CommandText = @"SELECT FileName FROM Import;";
fmd.CommandType = CommandType.Text;
SqlDataReader r = fmd.ExecuteReader();
while (r.Read())
{
ImportedFiles.Add(Convert.ToString(r["FileName"]));
}
}
}
return ImportedFiles;
}
private static void Main(string[] args)
{
using (SqlConnection con = new SqlConnection(@"Server=FRXSQLDEV;Database=RX_CMMData;Integrated Security=YES"))
{
con.Open();
using (SqlCommand insertCommand = con.CreateCommand())
{
Console.WriteLine("Connecting to SQL server...");
SqlCommand cmdd = con.CreateCommand();
string[] files = Directory.GetFiles(@"C:\Documents and Settings\js91162\Desktop\", "R.txt*", SearchOption.AllDirectories);
insertCommand.Parameters.Add(new SqlParameter("@FeatType", DbType.String));
insertCommand.Parameters.Add(new SqlParameter("@FeatName", DbType.String));
insertCommand.Parameters.Add(new SqlParameter("@Value", DbType.String));
insertCommand.Parameters.Add(new SqlParameter("@Actual", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@Nominal", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@Dev", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@TolMin", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@TolPlus", DbType.Decimal));
insertCommand.Parameters.Add(new SqlParameter("@OutOfTol", DbType.Decimal));
List<string> ImportedFiles = GetImportedFileList();
foreach (string file in files.Except(ImportedFiles))
{
var FileNameExt1 = Path.GetFileName(file);
cmdd.Parameters.Add(new SqlParameter("@FileExt", FileNameExt1));
cmdd.CommandText =
@"
IF (EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'RX_CMMData' AND TABLE_NAME = 'Import')) BEGIN SELECT COUNT(*) FROM Import WHERE FileName = @FileExt; END";
int count = Convert.ToInt32(cmdd.ExecuteScalar());
con.Close();
con.Open();
if (count == 0)
{
Console.WriteLine("Parsing CMM data for SQL database... Please wait.");
insertCommand.CommandText =
@"
INSERT INTO Import (FeatType, FeatName, Value, Actual, Nominal, Dev, TolMin, TolPlus, OutOfTol, PartNumber, CMMNumber, Date, FileName)
VALUES (@FeatType, @FeatName, @Value, @Actual, @Nominal, @Dev, @TolMin, @TolPlus, @OutOfTol, @PartNumber, @CMMNumber, @Date, @FileName);";
string FileNameExt = Path.GetFullPath(file);
string RNumber = Path.GetFileNameWithoutExtension(file);
string RNumberE = RNumber.Split('_')[0];
string RNumberD = RNumber.Split('_')[1];
string RNumberDate = RNumber.Split('_')[2];
DateTime dateTime = DateTime.ParseExact(RNumberDate, "yyyyMMdd", Thread.CurrentThread.CurrentCulture);
string cmmDate = dateTime.ToString("dd-MMM-yyyy");
string[] lines = File.ReadAllLines(file);
bool parse = false;
foreach (string tmpLine in lines)
{
string line = tmpLine.Trim();
if (!parse && line.StartsWith("Feat. Type,"))
{
parse = true;
continue;
}
if (!parse || string.IsNullOrEmpty(line))
{
continue;
}
Console.WriteLine(tmpLine);
foreach (SqlParameter parameter in insertCommand.Parameters)
{
parameter.Value = null;
}
string[] values = line.Split(new[] { ',' });
for (int i = 0; i < values.Length - 1; i++)
{
SqlParameter param = insertCommand.Parameters[i];
if (param.DbType == DbType.Decimal)
{
decimal value;
param.Value = decimal.TryParse(values[i], out value) ? value : 0;
}
else
{
param.Value = values[i];
}
}
}
insertCommand.Parameters.Add(new SqlParameter("@PartNumber", RNumberE));
insertCommand.Parameters.Add(new SqlParameter("@CMMNumber", RNumberD));
insertCommand.Parameters.Add(new SqlParameter("@Date", cmmDate));
insertCommand.Parameters.Add(new SqlParameter("@FileName", FileNameExt));
//
insertCommand.ExecuteNonQuery();
}
}
Console.WriteLine("CMM data successfully imported to SQL database...");
}
con.Close();
}
}
}
}
僅供參考 - 的部分號碼,CMMNumber,日期等從底部的文件名被拉到,我需要在它旁邊的桌子各自的記錄。
當我做insertCommand.Parameters.Clear(),代碼運行,但只有從每個txt文件的第一行被添加到數據庫.. ??? – 2010-05-28 05:18:30
@jake,這聽起來像你有一個循環的問題,你之前沒有注意到,因爲你的代碼在第二次迭代開始時出錯。 Parameters.Clear()不會導致你的代碼失敗,除非你把它放在錯誤的位置。在調試器下運行代碼,並開始檢查代碼流和本地變量的值,並查找循環可能提前結束的原因。 – slugster 2010-05-28 06:28:55
代碼不再失敗,它不會像每個文件的所有行一樣遍歷所有行。 (它使用sqlite),現在它完成了,但是當我檢查數據庫時,它只是從每個文件的第一行添加了部件號和數據......奇怪的交易......有意義嗎? – 2010-05-28 06:43:12