日期:2014-05-19  浏览次数:20815 次

为什么datatable使用acceptchange之后,对应的DataAdapter使用updata不能更新源数据库
如题,至少是插入时无效。
SqlConnection   discConn=new   SqlConnection( "Data   Source=.;User   Id=sa;PassWord=123;Initial   Catalog=Disc; ");
SqlDataAdapter   fileinfoDA=new   SqlDataAdapter( "select   *   from   fileinfo ",discConn);
SqlDataAdapter   folderinfoDA=new   SqlDataAdapter( "select   *   from   folderinfo ",discConn);
SqlDataAdapter   discinfoDA=new   SqlDataAdapter( "select   *   from   discinfo ",discConn);
SqlDataAdapter   usertableDA=new   SqlDataAdapter( "select   *   from   usertable ",discConn);
SqlDataAdapter   disckindDA=new   SqlDataAdapter( "select   *   from   disckind ",discConn);
//SqlCommandBuilder   userCB=new   SqlCommandBuilder(usertableDA);
DataSet   discDS=new   DataSet();
try
{
discConn.Open();
Console.WriteLine( "Succeed   connected! ");
fileinfoDA.Fill(discDS, "FileInfo ");
discinfoDA.Fill(discDS, "DiscInfo ");
folderinfoDA.Fill(discDS, "FolderInfo ");
usertableDA.Fill(discDS, "UserTable ");
disckindDA.Fill(discDS, "DiscKind ");
discDS.Tables[ "FileInfo "].PrimaryKey=new   DataColumn[]{discDS.Tables[ "FileInfo "].Columns[ "DiscNumber "],discDS.Tables[ "FileInfo "].Columns[ "FileFullName "]};
discDS.Tables[ "FolderInfo "].PrimaryKey=new   DataColumn[]{discDS.Tables[ "FolderInfo "].Columns[ "DiscNumber "],discDS.Tables[ "FolderInfo "].Columns[ "FolderFullName "]};
discDS.Tables[ "DiscInfo "].PrimaryKey=new   DataColumn[]{discDS.Tables[ "DiscInfo "].Columns[ "DiscNumber "]};
discDS.Tables[ "UserTable "].PrimaryKey=new   DataColumn[]{discDS.Tables[ "UserTable "].Columns[ "UserName "]};
discDS.Tables[ "DiscKind "].PrimaryKey=new   DataColumn[]{discDS.Tables[ "DiscKind "].Columns[ "DiscKind "]};
discDS.Relations.Add(discDS.Tables[ "DiscInfo "].Columns[ "DiscNumber "],discDS.Tables[ "FileInfo "].Columns[ "DiscNumber "]);
discDS.Relations.Add(discDS.Tables[ "DiscInfo "].Columns[ "DiscNumber "],discDS.Tables[ "FolderInfo "].Columns[ "DiscNumber "]);
discDS.Relations.Add(discDS.Tables[ "DiscKind "].Columns[ "DiscKind "],discDS.Tables[ "DiscInfo "].Columns[ "DiscKind "]);
discDS.Relations.Add(discDS.Tables[ "UserTable "].Columns[ "UserName "],discDS.Tables[ "DiscInfo "].Columns[ "Owner "]);
//DataSet   tempDS=discDS.Clone();
//tempDS.WriteXmlSchema( "tempDS_Schema.xml ");
//tempDS.WriteXml( "tempDS.xml ");
//discDS.WriteXml( "discDS.xml ");
usertableDA.InsertCommand=new   SqlCommand( "insert   into   UserTable(UserName,UserPW)   values(@UserName,@UserPW) ",discConn);
SqlParameter   usernameParm=usertableDA.InsertCommand.Parameters.Add( "@UserName ",SqlDbType.Char,20, "UserName ");
SqlParameter   userpwParm=usertableDA.InsertCommand.Parameters.Add( "@UserPW ",SqlDbType.Char,10, "UserPW ");
DataRow   userRow=discDS.Tables[ "UserTable "].NewRow();
userRow[ "UserName "]= "hello ";