Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to copy data from one table to another using sqlbulkcopy but i found an error as user was not associated with trusted sql connection

my code is

public static void PerformBulkCopy()
    {
        string connectionString = "Data Source=localhost;database=test;Trusted_Connection=true";

        {
            using (SqlConnection sourceConnection = new SqlConnection(connectionString))
            {
                SqlCommand myCommand = new SqlCommand("select * from odi", sourceConnection);
                sourceConnection.Open();
                SqlDataReader myReader = myCommand.ExecuteReader();

                using (SqlConnection destinationConnection = new SqlConnection(connectionString))
                {
                    destinationConnection.Open();

                    using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString))
                    {
                        bulkCopy.BatchSize = 50;
                        bulkCopy.NotifyAfter = 50;
                        bulkCopy.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);
                        bulkCopy.DestinationTableName = "odi1";
                        bulkCopy.WriteToServer(myReader);
                    }
                    destinationConnection.Close();
                }
                myReader.Close();
                sourceConnection.Close();
            }
            
        }
    }

give me the solution please
Posted
Updated 11-Apr-11 3:07am
v2

Your connection string is wrong, you don't specify user name and password when using a Trusted Connection

http://www.connectionstrings.com/sql-server-2005[^]

If you want to use a trusted connection[^], you need to make sure your windows account has sufficient permissions on the server

Follow the instructions for step 'A Windows account with insufficient permissions'
 
Share this answer
 
v2
Comments
beginner in C#.net 11-Apr-11 9:54am    
thank u friend i got solution...
Kim Togo 11-Apr-11 9:56am    
Agree. My 5.
This can sometimes be fixed by setting the sql server's security mode, under authentication, to SQLServer And Windows.

Hope this helps.
 
Share this answer
 
Comments
beginner in C#.net 11-Apr-11 9:10am    
i done data source=localhost;database=dname;trusted_connection=true..
again i found error.. in server we cudn't copy
Make sure you have the correct database connection string.

Something like this.

string connectionString = "Data Source=localhost;database=test;user id= xxxxxx; password = yyyyyy";
 
Share this answer
 
Your Connection string is wrong please check it.
 
Share this answer
 
Comments
nagendrathecoder 11-Apr-11 9:16am    
This is not an answer, you should have posted it as a comment.

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