Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Save data into sql table

Author  Topic 

JmaC
Starting Member

18 Posts

Posted - 2010-08-03 : 23:10:24
hello everyone....

i write a simple coding using C# to CSV parser to read and delimit comma from text files and view it into data grid view...
Now i trying to save that data(which delimit from txt file into data grid) into SQL table...Please get some help...

here is the code:
<code>
try
{

if (string.IsNullOrEmpty(txtOpenFile.Text.Trim()))
{
MessageBox.Show("Please select file");
return;
}
System.IO.StreamReader sr = new System.IO.StreamReader(txtOpenFile.Text.Trim());
string inputLine = "";

DataTable dt = null;

DataRow row;
while ((inputLine = sr.ReadLine()) != null)
{
string[] array;
array = inputLine.Split(',');

if (dt == null)
{
dt = new DataTable();

foreach (string element in array)
dt.Columns.Add();
}

row = dt.NewRow();

for (int i = 0; i < array.Length; i++)
{
row[i] = array[i];
}

dt.Rows.Add(row);
}

dt.Rows.RemoveAt(0);
dataGridView1.DataSource = dt;

sr.Close();
}
catch (Exception)
{
throw;
}


</code>

Thanks & Regards
JmaC

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-08-24 : 05:19:36
You can directly add data to a table using BULK INSERT
http://beyondrelational.com/blogs/madhivanan/archive/2010/03/17/bulk-insert-to-table-with-specific-columns.aspx

or if you want to copy grid's data to a table, you need to do it via C#. Refer www.asp.net

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -