sqlce的新手。我花了兩天的時間試圖找出這個問題,並在類似的問題上搜索stackoverflow。 如何利用sqlce從一個sdf數據庫複製表(僅適用於模式和數據)到另一個sdf數據庫?我應該尋找哪些sqlce關鍵詞?任何意見,讚賞,任何代碼更是如此。將sdf表從一個數據庫複製到C#中的另一個數據庫與sql ce
0
A
回答
0
這個運行的腳本是什麼我趕上了與:需要sql server compact nuget
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlServerCe;
namespace ConsoleApp5 {
class Program {
static void Main(string[] args) {
string tableName = "ZTVC_MF_S_VALCHA";
string sourceString = "Data Source = C:\\Users\\David\\source\\repos\\ConsoleApp5\\ConsoleApp5\\bin\\Debug\\XXX.sdf";
string destString = "Data Source = C:\\Users\\David\\source\\repos\\ConsoleApp5\\ConsoleApp5\\bin\\Debug\\YYY.sdf";
using (SqlCeConnection sourceConn = new SqlCeConnection()) {
SqlCeCommand sourceCmd = sourceConn.CreateCommand();
sourceCmd.CommandType = CommandType.Text;
sourceCmd.Parameters.AddWithValue("tableName", tableName);
sourceConn.ConnectionString = sourceString;
sourceConn.Open();
using (SqlCeConnection destConn = new SqlCeConnection()) {
destConn.ConnectionString = destString;
destConn.Open();
DoesTableExist(tableName, destConn);
DataTable dt=GetDataFromTable(tableName, sourceConn);
CopyTableSchema(tableName, dt, destConn);
FillTableCopy(tableName, dt, destConn);
DeleteTable(tableName, destConn);
Console.ReadKey();
}
}
}
private static void FillTableCopy( string tabName, DataTable dt, SqlCeConnection destConn) {
List<string> dTypesList = new List<string>();
int dTypesListIndexCounter = 0;
string sqlCmd = "";
foreach (DataRow row in dt.Rows) {
sqlCmd = "INSERT INTO " + tabName + "(";
int colHeadCounter = 0;
int colHeadLast = dt.Columns.Count;
foreach (DataColumn colHead in dt.Columns) {
string dType = colHead.DataType.ToString().ToLower().Substring(7);
dTypesList.Add(dType);
if (++colHeadCounter == colHeadLast) {
sqlCmd += colHead + " ";
}
else {
sqlCmd += colHead + ", ";
}
}
sqlCmd += ") VALUES(";
//Console.Write(sqlCmd);
colHeadCounter = 0;
colHeadLast = row.ItemArray.Length;
foreach (var item in row.ItemArray) {
var temp = item.ToString();
if (temp.Count() == 0) {
if (++colHeadCounter == colHeadLast) {
sqlCmd += ("null ");
}else {
sqlCmd += ("null, ");
}
}else if (dTypesList[dTypesListIndexCounter] == "string"
|| dTypesList[dTypesListIndexCounter] == "char"
|| dTypesList[dTypesListIndexCounter] == "datetime"
|| dTypesList[dTypesListIndexCounter] == "single"
|| dTypesList[dTypesListIndexCounter] == "guid") {
if (++colHeadCounter == colHeadLast) {
sqlCmd += ("'" + item + "' ");
}else {
sqlCmd += ("'" + item + "', ");
}
}else {
if (++colHeadCounter == colHeadLast) {
sqlCmd += (item + " ");
}else {
sqlCmd += (item + ", ");
}
}
dTypesListIndexCounter++;
}
sqlCmd += ")";
//Console.Write(sqlCmd);
//Console.WriteLine();
SqlCeCommand cmd = new SqlCeCommand(sqlCmd, destConn);
cmd.ExecuteNonQuery();
}
//Console.WriteLine(cmd);
Console.WriteLine("Data copy executed.");
}
private static void DeleteTable(string tableName, SqlCeConnection pathConn) {
string sqlCmd = "DROP TABLE " + tableName;
SqlCeCommand cmd = new SqlCeCommand(sqlCmd, pathConn);
cmd.ExecuteNonQuery();
Console.WriteLine(tableName + " deleted.");
}
private static void CopyTableSchema(string tableName, DataTable dt, SqlCeConnection destConn) {
string sqlCmd = "Create table " + tableName + "(";
int colHeadCounter = 0;
int colHeadLast = dt.Columns.Count;
/*sqlce supported types: bigint, integer,smallint,tinyint,bit,numeric, money, float, real, datetime,
* national character, national character varying, ntext, nchar, binary,varbinary, image, uniqueidentifier,
* identity, rowguidcol, timestamp/rowversion */
foreach (DataColumn colHead in dt.Columns) {
string dType = colHead.DataType.ToString().ToLower().Substring(7);
////still needs all type conversions mapped
if (dType == "int16") {
dType = "smallint";
}
else if (dType == "int32") {
dType = "int";
}
else if (dType == "int64") {
dType = "bigint";
}
else if (dType == "string") {
dType = "nvarchar(4000)";
}
else if (dType == "boolean") {
dType = "bit";
}
else if (dType == "byte") {
dType = "tinyint";
}
else if (dType == "byte[]") {
dType = "binary";
}
else if (dType == "char") {
dType = "nchar";
}
else if (dType == "datetime") {
dType = "datetime";
}
else if (dType == "decimal") {
dType = "money";
}
else if (dType == "double") {
dType = "float";
}
else if (dType == "sbyte") {
dType = "tinyint";
}
else if (dType == "single") {
dType = "real";
}
else if (dType == "guid") {
dType = "uniqueidentifier";
}
else {
Console.WriteLine();
Console.WriteLine("Add new type to type conversion");
Console.ReadKey();
Environment.Exit(0);
}
if (++colHeadCounter == colHeadLast) {
sqlCmd += colHead + " " + dType;
}
else {
sqlCmd += colHead + " " + dType + ", ";
}
}
sqlCmd += ")";
//Console.WriteLine(sqlCmd);
SqlCeCommand cmd = new SqlCeCommand(sqlCmd, destConn);
cmd.ExecuteNonQuery();
Console.WriteLine("Copy of " + tableName + " created.");
}
private static DataTable GetDataFromTable(string tableName, SqlCeConnection sourceConn) {
SqlCeCommand sourceCmd = sourceConn.CreateCommand();
sourceCmd.CommandText = "Select * from " + tableName;
SqlCeDataAdapter sourceAdp = new SqlCeDataAdapter(sourceCmd);
DataTable dt = new DataTable();
sourceAdp.Fill(dt);
//Console.WriteLine("The SqlCeDataAdapter succesfully filled " + dt.Rows.Count + " rows in the DataSet!");
return dt;
}
private static bool DoesTableExist(string tableName, SqlCeConnection destConn) {
bool doesIt = false;
SqlCeCommand destCmd = destConn.CreateCommand();
destCmd.CommandType = CommandType.Text;
destCmd.CommandType = CommandType.Text;
destCmd.CommandText = "SELECT 1 FROM Information_Schema.Tables WHERE TABLE_NAME = @tableName";
destCmd.Parameters.AddWithValue("tableName", tableName);
object result = destCmd.ExecuteScalar();
if (result != null) {
doesIt = true;
}
return doesIt;
}
}
}
+0
我不得不返回並添加一些代碼以處理基準字符串中的單引號 – AlanAxis
0
你可以用我的SQL Server Compact工具箱在Visual Studio(或命令行),以腳本模式和表從一個SDF文件,也對其他SDF文件
相關問題
- 1. 將數據從一個數據庫表複製到另一個數據庫表?
- 2. Sql將表從一個數據庫複製到另一個數據庫
- 3. 複製/同步SQL Server [MDF]數據庫到CE數據庫[SDF]
- 4. SQL Server將表從一個數據庫複製到另一個
- 5. 將表格數據從一個數據庫複製到另一個數據庫
- 6. 在PostgreSQL中將表從一個數據庫複製到另一個數據庫
- 7. 將一列數據從一個數據庫複製到另一個數據庫
- 8. 將SQL Server數據庫複製到另一個數據庫
- 9. 將數據庫從一個SQL Server複製到另一個
- 10. 如何將表從一個數據庫複製到另一個數據庫?
- 11. 將表從一個數據庫複製到另一個數據庫
- 12. 將表從一個數據庫複製到另一個數據庫
- 13. LLBLGen:將表從一個數據庫複製到另一個數據庫
- 14. 將數據從一個數據庫複製到另一個數據庫
- 15. 將數據從一個數據庫複製到另一個數據庫
- 16. 將數據從一個數據庫複製到另一個數據庫
- 17. 將數據從一個數據庫複製到另一個數據庫
- 18. Django:將數據從一個數據庫複製到另一個數據庫
- 19. 訪問將數據從一個數據庫複製到另一個數據庫
- 20. 將數據從一個數據庫複製到另一個數據庫
- 21. 在SQL Server中將龐大的表數據從一個數據庫複製到另一個數據庫
- 22. 從一個數據庫表複製到另一個C#
- 23. SQL數據庫遷移 - 性能問題(將數據從一個數據庫複製到另一個數據庫)
- 24. 將數據從1個數據庫表複製到另一個的SQL代碼
- 25. 將三列從一個數據庫表複製到另一個
- 26. 將數據從一個表複製到另一個不同的數據庫中
- 27. 將標量函數從一個數據庫複製到另一個數據庫
- 28. 將PostgreSQL數據庫複製到另一個數據庫中
- 29. 將數據從一個數據庫複製到另一個SQL代碼
- 30. Sql Server將數據從一個數據庫表複製到另一個數據庫
您可能必須使用該表的CREATE查詢將它在目標數據庫中。同樣,源表中的一個循環將記錄複製到目標數據庫中 – shahsani