SqlBulkCopy 无法访问目标表“NEWS_PRESS1”。

liuliwen137 2011-12-30 11:18:48
利用SqlBulkCopy 从服务器复制数据到本机 为什么无法访问本机的表 这是怎么回事? 是权限不够吗? 还是什么? 权限不够的话怎么设置.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace Webtest
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strsql = "select top 500000 Guid, NewsID, BigClassName, SmallClassName, Title, Corp, Author, StockCode, Original, UpdateTime, Content from NEWS_PRESS";

string connstring = "Data Source=服务器的连接 IP ;Database=服务器的数据库;Persist Security Info=True;User ID=ReportDBWriter;Password=#8AghRC]=r}^7M;MultipleActiveResultSets=True";

string connstring1 = "Data Source=. 本机的;Initial Catalog=NewsDB;Persist Security Info=True;User ID=sa;Password=sasasa;MultipleActiveResultSets=True";

DataTable dt = getStrToDt(connstring, strsql);

if (dt != null && dt.Rows.Count > 0)
{
AddDataTableToDB(connstring1, dt, "NEWS_PRESS1");
}

}
}

public bool AddDataTableToDB(string sDBconn, DataTable source, string tableName)
{
SqlTransaction tran = null;//声明一个事务对象.
try
{
using (SqlConnection conn = new SqlConnection(sDBconn))
{
conn.Open();//打开链接.
using (tran = conn.BeginTransaction())
{
using (SqlBulkCopy copy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, tran))
{
copy.DestinationTableName = tableName; //指定服务器上目标表的名称.
copy.BulkCopyTimeout = 1000;
copy.WriteToServer(source); //执行把DataTable中的数据写入DB.
tran.Commit(); //提交事务.
return true; //返回True 执行成功!
}

}
}
}
catch (Exception ex)
{
if (null != tran)
tran.Rollback();
return false;//返回False 执行失败!
}
}

public DataTable getStrToDt(string connString, string sql)
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
return ds.Tables[0];
}

}
}

报出异常 无法访问目标表“NEWS_PRESS1”。
...全文
715 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
BossFriday 2011-12-30
  • 打赏
  • 举报
回复
关于批量插入的介绍网上一大把,这种问题要自己分析和解决。
  • 打赏
  • 举报
回复
connstring1对应的数据库不存在这张表。
或是该表有架构,你没有加上架构名称。
下面的控制台应用程序演示如何使用 SqlBulkCopy 类加载数据。在此示例中,使用 SqlDataReader 将数据从 SQL Server 2005 AdventureWorks 数据库中的 Production.Product 复制到同一数据库中的相似。 重要说明: 在您按照批量复制示例设置 (ADO.NET) 中的描述创建工作之后,此示例才会运行。提供此代码仅为了演示使用 SqlBulkCopy 的语法。如果源目标都在同一个 SQL Server 实例中,则使用 Transact-SQL INSERT … SELECT 语句复制数据会更方便快捷。 using System.Data.SqlClient; class Program { static void Main() { string connectionString = GetConnectionString(); // Open a sourceConnection to the AdventureWorks database. using (SqlConnection sourceConnection = new SqlConnection(connectionString)) { sourceConnection.Open(); // Perform an initial count on the destination table. SqlCommand commandRowCount = new SqlCommand( "SELECT COUNT(*) FROM " + "dbo.BulkCopyDemoMatchingColumns;", sourceConnection); long countStart = System.Convert.ToInt32( commandRowCount.ExecuteScalar()); Console.WriteLine("Starting row count = {0}", countStart); // Get data from the source table as a SqlDataReader. SqlCommand commandSourceData = new SqlCommand( "SELECT ProductID, Name, " + "ProductNumber " + "FROM Production.Product;", sourceConnection); SqlDataReader reader = commandSourceData.ExecuteReader(); // Open the destination connection. In the real world you would // not use SqlBulkCopy to move data from one table to the other // in the same database. This is for demonstration purposes only. using (SqlConnection destinationConnection = new SqlConnection(connectionString)) { destinationConnection.Open(); // Set up the bulk copy object. // Note that the column positions in the source // data reader match the column positions in // the destination table so there is no need to // map columns. using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection)) { bulkCopy.DestinationTableName = "dbo.BulkCopyDemoMatchingColumns"; try { // Write from the source to the destination. bulkCopy.WriteToServer(reader); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { // Close the SqlDataReader. The SqlBulkCopy // object is automatically closed at the end // of the using block. reader.Close(); } } // Perform a final count on the destination // table to see how many rows were added. long countEnd = System.Convert.ToInt32( commandRowCount.ExecuteScalar()); Console.WriteLine("Ending row count = {0}", countEnd); Console.WriteLine("{0} rows were added.", countEnd - countStart); Console.WriteLine("Press Enter to finish."); Console.ReadLine(); } } } private static string GetConnectionString() // To avoid storing the sourceConnection string in your code, // you can retrieve it from a configuration file. { return "Data Source=(local); " + " Integrated Security=true;" + "Initial Catalog=AdventureWorks;"; } }

110,618

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧