错误用户未与受信任的SQL连接相关联
问题描述:
我想使用sqlbulkcopy
将数据从一个表复制到另一个表,但是我发现一个错误,因为用户未与受信任的sql连接相关联
我的代码是
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
答
您的连接字符串错误,使用受信任的连接时您未指定用户名和密码
http://www.connectionstrings.com/sql-server-2005 [受信任的连接 [
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''
有时可以通过将身份验证下的sql服务器的安全模式设置为SQLServer和Windows来解决此问题.
希望对您有所帮助.
This can sometimes be fixed by setting the sql server''s security mode, under authentication, to SQLServer And Windows.
Hope this helps.
确保您具有正确的数据库连接字符串.
这样的事情.
Make sure you have the correct database connection string.
Something like this.
string connectionString = "Data Source=localhost;database=test;user id= xxxxxx; password = yyyyyy";