【急】SQL Server数据导入Compact数据库

anbin0814 anbin0814 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

private static void CopyTable(SqlConnection sqlConn, SqlCeConnection ceConn, string queryString, string destTableName, bool delTalbe)
{
sqlConn.Open();
ceConn.Open();
SqlCommand sqlCmd = sqlConn.CreateCommand();
sqlCmd.CommandText = queryString;

SqlCeCommand ceCmd = ceConn.CreateCommand();
ceCmd.CommandType = CommandType.TableDirect;
ceCmd.CommandText = destTableName;

SqlDataReader sqlReader = sqlCmd.ExecuteReader();

SqlCeResultSet resultSet = ceCmd.ExecuteResultSet(
ResultSetOptions.Sensitive | //检测对数据源所做的更改
ResultSetOptions.Scrollable | //可以向前或向后滚动
ResultSetOptions.Updatable); //允许更新数据

if (delTalbe)
{
string sql = "delete " + destTableName;

SqlCeCommand cmd = ceConn.CreateCommand();
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
}

object[] values;

SqlCeUpdatableRecord record;

while (sqlReader.Read())
{
//从源数据库表中读取数据
values = new object[sqlReader.FieldCount];
sqlReader.GetValues(values);

//把数据写入到Conpact数据库
record = resultSet.CreateRecord();
record.SetValues(values);
resultSet.Insert(record);
}

sqlReader.Close();
resultSet.Close();

sqlConn.Close();
ceConn.Close();

}



CREATE TABLE [dbo].[test](
[id] [int] NOT NULL,
[order] [int] NULL,
CONSTRAINT [PK_test] PRIMARY KEY CLUSTERED )



resultSet.Insert(record);这句老是报错 Unspecified error [ test ]

"SQL Server Compact ADO.NET Data Provider"
" at System.Data.SqlServerCe.SqlCeDataReader.ProcessResults(Int32 hr)\r\n at System.Data.SqlServerCe.SqlCeResultSet.SetValues(SEPREPAREMODE mode, SqlCeUpdatableRecord record)\r\n at System.Data.SqlServerCe.SqlCeResultSet.InternalInsert(Boolean fMoveTo, Object sender, SqlCeUpdatableRecord record)\r\n at System.Data.SqlServerCe.SqlCeResultSet.Insert(SqlCeUpdatableRecord record)\r\n at SmartMobileDevice.DAL.DataSynTools.CopyTable(SqlConnection sqlConn, SqlCeConnection ceConn, String queryString, String destTableName, Boolean delTalbe)\r\n at SmartMobileDevice.DAL.DataSynTools.Syn()\r\n at SmartMobileDevice.Login.button1_Click(Object sender, EventArgs e)\r\n at System.Windows.Forms.Control.OnClick(EventArgs e)\r\n at System.Windows.Forms.Button.OnClick(EventArgs e)\r\n at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)\r\n at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)\r\n at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)\r\n at System.Windows.Forms.Application.Run(Form fm)\r\n at SmartMobileDevice.Program.Main()\r\n"
...全文

相关推荐

评论
收藏 已收藏
分享
返回顶部
anbin0814
嵌入开发(WinCE) 19,524 成员 41,565 内容