我必须将数据从 ms 访问插入到 sql server。我的逻辑是:
获取 ms 访问中的名称列表。
插入前禁用外键和触发器。
阅读每个表格。
通过 SetAdded() 方法更改每一行的 RowState。
最后使用命令生成器插入它
错误
表(Ms Access)中的一个字段具有数据类型日期/时间。但它只有时间值 NO DATE 部分。在插入过程中会出错。" SqlDateTime 溢出。必须在 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。"
在数据集可视化工具中,我看到了 12/30/1899 1:01:01 AM 的值。
如何解决它。
try
{
oleCon = new OleDbConnection();
oleCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Client Work\Client Docs\Don\ABCMotor.mdb;User Id=admin;Password=;";
oleCon.Open();
dt = new DataTable();
dt = oleCon.GetSchema("tables");
sqlCon = new SqlConnection(@"Server=ACER-PC\SQLEXPRESS2008R2;Database=ABC;Integrated Security=SSPI;");
sqlCon.Open();
SqlCommandBuilder sqlCmdB = new SqlCommandBuilder();
sqlCmd = new SqlCommand();
SqlTransaction sqlTran;
sqlTran = sqlCon.BeginTransaction();
sqlCmd.Connection = sqlCon;
sqlCmd.Transaction = sqlTran;
sqlCmd.CommandText = "sp_msforeachtable";
sqlCmd.Parameters.AddWithValue("@Command1", "ALTER TABLE ? NOCHECK CONSTRAINT all");
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.ExecuteNonQuery();
sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.Transaction = sqlTran;
sqlCmd.CommandText = "sp_msforeachtable";
sqlCmd.Parameters.Clear();
sqlCmd.Parameters.AddWithValue("@Command1", "ALTER TABLE ? DISABLE TRIGGER all");
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.ExecuteNonQuery();
foreach (DataRow row in dt.Rows)
{
if (row["Table_Name"].ToString().StartsWith("MSys") == false)
{
oleDa = new OleDbDataAdapter("Select * from [" + row["Table_Name"] + "]", oleCon);
ds = new DataSet();
oleDa.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
sqlDa = new SqlDataAdapter("Select * from [" + row["Table_Name"] + "]", sqlCon);
sqlDa.SelectCommand.Transaction = sqlTran;
foreach (DataRow item in ds.Tables[0].Rows)
{
item.SetAdded();
}
sqlCmdB.DataAdapter = sqlDa;
sqlCmdB.GetInsertCommand();
sqlDa.Update(ds);
}
}
}
sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.Transaction = sqlTran;
sqlCmd.CommandText = "sp_msforeachtable";
sqlCmd.Parameters.Clear();
sqlCmd.Parameters.AddWithValue("@Command1", "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all");
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.ExecuteNonQuery();
sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlCmd.Transaction = sqlTran;
sqlCmd.CommandText = "sp_msforeachtable";
sqlCmd.Parameters.Clear();
sqlCmd.Parameters.AddWithValue("@Command1", "ALTER TABLE ? ENABLE TRIGGER all");
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.ExecuteNonQuery();
sqlTran.Commit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}