111,120
社区成员
发帖
与我相关
我的任务
分享
//做一个同步数据表问题
SqlConnection LocalConnection = null;
try
{
DataTable LocalTable = new DataTable();
SqlDataAdapter UpdateAdapter = new SqlDataAdapter();
LocalConnection = new SqlConnection();
SqlCommand SelectCommand = new SqlCommand();
SqlCommand UpdateCommand = new SqlCommand();
SqlCommand InsertCommand = new SqlCommand();
LocalConnection.ConnectionString = config.LocalConnection;
SelectCommand.CommandText = "Select * From QuoteTableName";
SelectCommand.CommandType = CommandType.Text;
SelectCommand.Connection = LocalConnection;
UpdateCommand.CommandText = "Update QuoteTableName Set Field1=@Field1,Field2=@Field2 Where PramyKey=@PramyKey";
UpdateCommand.CommandType = CommandType.Text;
UpdateCommand.Connection = LocalConnection;
InsertCommand.CommandText = "Insert Into QuoteTableName Values(@PramyKey,@Field1,@Field2)";
InsertCommand.CommandType = CommandType.Text;
InsertCommand.Connection = LocalConnection;
SqlParameter parameter1=new SqlParameter("@field1",SqlDbType.NVarchar,8,"field1");
InsertCommand.Parameters.Add(parameter1);
UpdateCommand.Parameters.Add(parameter1);
SqlParameter parameter2=new SqlParameter("@field2",SqlDbType.NVarchar,8,"field2");
InsertCommand.Parameters.Add(parameter2);
UpdateCommand.Parameters.Add(parameter2);
SqlParameter parameter3=new SqlParameter("@PramyKey",SqlDbType.NVarchar,8,"PramyKey");
InsertCommand.Parameters.Add(parameter3);
UpdateCommand.Parameters.Add(parameter3);
UpdateAdapter.SelectCommand = SelectCommand;
UpdateAdapter.UpdateCommand = UpdateCommand;
UpdateAdapter.InsertCommand = InsertCommand;
UpdateAdapter.Fill(LocalTable);
DataTable SourceTable=GetSourceTable();
//更新本地数据表---------------------------------疑问地方
foreach (DataRow row in SourceTable.Rows)
{
LocalTable.ImportRow(row);
}
UpdateAdapter.UpdateBatchSize =10;
UpdateCommand.UpdatedRowSource = UpdateRowSource.None;
InsertCommand.UpdatedRowSource = UpdateRowSource.None;
SelectCommand.UpdatedRowSource = UpdateRowSource.None;
LocalConnection.Open();
UpdateAdapter.Update(LocalTable);//提交更新了。为什么无法真正更新到数据源
LocalTable.Clear();
}
catch (Exception ex)
{
LogHelper.Write("同步数据源表异常"+ex.ToString());
}
finally
{
if (LocalConnection.State == ConnectionState.Open)
{
LocalConnection.Close();
}
}
MainDataSet mds = new MainDataSet(); MainDataSetTableAdapters.TSelfTableAdapter tslef = new ABBS.MainDataSetTableAdapters.TSelfTableAdapter();
//更新本地数据表---------------------------------疑问地方
foreach (DataRow row in SourceTable.Rows)
{
LocalTable.ImportRow(row);
}
string file = this.openFileDialog1.FileName;
//this.label1.Text = file;
string[] texts = File.ReadAllLines(file, Encoding.Default);
MainDataSet mds = new MainDataSet();
MainDataSetTableAdapters.TSelfTableAdapter tslef = new ABBS.MainDataSetTableAdapters.TSelfTableAdapter();
for (int i = 0; i < texts.Length;i++ )
{
MainDataSet.TSelfRow row = (MainDataSet.TSelfRow)mds.TSelf.NewRow();
row.IdPrem = price;//价格
row.IdNo = texts[i].Substring(0, 14);//号码
row.status = 1;//状态
mds.TSelf.Rows.Add(row);//添加行
}
int j = tslef.Update(mds.TSelf);//更新