Hi, can anyone help me in storing a text file data to database using C# coding. I'm not sure of the below code can anyone correct me.
Thanks in advance.
Note:
TextFile1.txt
124 Keer 22 14000.000000
125 gjh 45 12103.000000
126 sg 48 63230.000000
129 fdgd 21 65620.000000
con = new SqlConnection();
con .ConnectionString="Server= SQLEXPRESS;integrated security = true;initial catalog = Practice;";
con.Open();
cmd = con.CreateCommand();
foreach (string line in File.ReadAllLines("TextFile1.txt"))
{
string[] parts = line.Split(' ');
int i = 0;
foreach (string part in parts)
{
cmd.CommandText = "Insert into BankDetails values(@acc,@nm,@ag,@bal)";
cmd.Parameters.Add("@acc", SqlDbType.NVarChar).Value = part[i];
cmd.Parameters.Add("@nm", SqlDbType.NVarChar).Value = part[i];
cmd.Parameters.Add("@ag", SqlDbType.Int).Value = part[i];
cmd.Parameters.Add("@bal", SqlDbType.Decimal).Value = part[i].ToString();
} i++;
}
int x =cmd.ExecuteNonQuery();
if(x==1)
MessageBox.Show("Inserted to DB");
else
MessageBox.Show("Not Inserted");
con.Close();