Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem with my code is that
it is running without any error in VisualStudio but values of text file are not import into sql database table...
here is my code attached

C#
private void StartImport()
    {
        SqlBulkCopy bulkCopy = new SqlBulkCopy("Server=TCCLPC47;Database=aa;Trusted_Connection=True;", SqlBulkCopyOptions.TableLock);
        bulkCopy.DestinationTableName = "dbo.b";
        bulkCopy.WriteToServer(CreateDataTableFromFile());
    }
    private DataTable CreateDataTableFromFile()
        {
            DataTable dt = new DataTable();
            DataColumn dc;
            DataRow dr;
            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.int");
            dc.ColumnName = "c1";
            dc.Unique = false;
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.int");
            dc.ColumnName = "c2";
            dc.Unique = false;
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.int");
            dc.ColumnName = "c3";
            dc.Unique = false;
            dt.Columns.Add(dc);
            dc = new DataColumn();
            dc.DataType = System.Type.GetType("System.int");
            dc.ColumnName = "c4";
            dc.Unique = false;
            dt.Columns.Add(dc);
            FileStream fileStream = new FileStream(@"d:\A.txt", FileMode.Open);
            StreamReader sr = new StreamReader(@"d:\\A.txt");
            string input;
          while ((input = sr.ReadLine()) !=null)
            {
                string[] s = input.Split(new char[] { ',' });
            
                dr = dt.NewRow();
                dr["c1"] = s[0];
                dr["c2"] = s[1];
                dr["c3"] = s[2];
                dr["c4"] = s[3];
                dt.Rows.Add(dr);
                
            }
            sr.Close();
            return dt;
        }
    }


please help..
confused geminien..
Posted
Updated 18-May-10 18:24pm
v2

May be problem is on this line [ bulkCopy.DestinationTableName = "dbo.b"; ] this should be [bulkCopy.DestinationTableName = "databasename.dbo.b"; Or "databasename..b"] for more information check this link.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.destinationtablename.aspx[^]
 
Share this answer
 
hi friend,

Try this query,
bulk insert into [tablename] form 'File Path with Name and Extension'
{
FIELDSEPRATOR = ',' ,
FIELDTERMINATOR = '\N'
)

I hope It will help u,

thanks,
Mahesh Patel
 
Share this answer
 
Thanxs Mahesh..
can u plzz tell me where ll i write this code?i mean in visual studio on(aspx.cs) page?
 
Share this answer
 
v2
Comments
Christian Graus 21-May-10 21:25pm    
Don't push answer to post questions. Edit your post, or push comment, as I did. I'd suggest if you can't work out where to put this, then you really need to do some learning. They are giving you SQL, not code behind code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900