Import TEXT File into SQL Database

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I was trying to import textfile into SQL Database. However, I got an error. I have indicated the error. static void Main(string[] args)
{
SqlConnection thisConnection = new SqlConnection(@"Data Source=MZSGCHTE02;Initial Catalog=LogFilesTest;Integrated Security=True;MultipleActiveResultSets=True");
SqlCommand nonqueryCommand = thisConnection.CreateCommand();
try
{
thisConnection.Open();
nonqueryCommand.CommandText = "CREATE TABLE TestTable131 ( num1 int,num2 int)";
nonqueryCommand.ExecuteNonQuery();
nonqueryCommand.CommandText = "INSERT INTO TestTable131 VALUES (@num1,@num2)";
nonqueryCommand.Parameters.Add("@num1"); //The SqlParameterCollection only accepts non-null SqlParameter type objects, not String objects.
nonqueryCommand.Parameters.Add("@num2");
string[] allLines = File.ReadAllLines("C:\Users\ozk\desktop\text1.txt");
{
for (int i = 1; i < allLines.Length; i++)
{
string[] items = allLines.Split(new char[] { });
nonqueryCommand.Parameters["@num1"].Value = items[0];
nonqueryCommand.Parameters["@num2"].Value = items[1];
nonqueryCommand.ExecuteNonQuery();
}
}
}
finally
{
thisConnection.Close();
}
}

I got the error in herenonqueryCommand.Parameters.Add("@num1");



KAdir

View the full article
 
Back
Top