首页 新闻 赞助 找找看

SqlDataAdapter.Update(DataSet Ds)更新数据库

0
悬赏园豆:10 [已解决问题] 解决于 2013-09-27 11:36

随着项目需求的扩大,现在Employee表里新增了一个字段InitialSeniority    int,假设即这张表里    EmployeeId,Name,EnrollmentTime,InitialSeniority    四个字段。

现在我需要从excel里导入数据,excel表里有  EmployeeId和InitialSeniority 两个字段,大量数据。

 // jia zai  excel
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + excelFile + ";" + "Extended Properties=Excel 8.0;";
 DataSet ds = new DataSet();
                OleDbConnection conn = new OleDbConnection(strConn);
                conn.Open();
                string strExcel = "";
                OleDbDataAdapter myCommand = null;
                strExcel = string.Format("select * from [{0}$]", sheetName);
                myCommand = new OleDbDataAdapter(strExcel, strConn);
                myCommand.Fill(ds, sheetName);
                DataTable _tableExcel = ds.Tables[0];
                _tableExcel.PrimaryKey = new DataColumn[] { _tableExcel.Columns["AgentId"] };


// upadte operation
string strSql = @"select * from Employee";
                using (SqlConnection sqlconn = new SqlConnection(connectionString))
                {
                    SqlDataAdapter adapter = new SqlDataAdapter(strSql, sqlconn);
                     dsEmployee = new DataSet();
                                     
                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

                    DataTable _tableEmployee = dsEmployee.Tables["Employee"];
                    _tableEmployee.PrimaryKey = new DataColumn[] { _tableEmployee.Columns["EmployeeId"] };
                    string strSqlUpate = "UPDATE " + "'" + _tableEmployee + "'" + " SET " + "'" + _tableEmployee.Columns["InitialSeniority"] + "'" + " = " + "'" + _tableExcel.Columns[1] + "'" + " from " + "'" + _tableExcel + "'" +" WHERE " + "'" + _tableEmployee.Columns["EmployeeId"] + "'" + "= " + "'" + _tableExcel.Columns[0] + "'";

                    SqlCommand command = new SqlCommand(strSqlUpate, sqlconn);
                    
                   // SqlCommand upCmd = new SqlCommand("update [" + strTableName + "] set validFlag=@validFlag where email=@email", myConn);
                    //upCmd.Parameters.Add("@validFlag", SqlDbType.Int, 8, "validFlag");
                    //upCmd.Parameters.Add("@email", SqlDbType.NVarChar, 100, "email");
                    //emailAdapter.UpdateCommand = upCmd;
                    adapter.UpdateCommand = command;
                    adapter.Fill(dsEmployee, "Employee");
                    sqlconn.Close();
                    
                    adapter.Update(dsEmployee, "Employee");
                    dsEmployee.AcceptChanges();
                } 

我现在的代码如上,无法实现我想要的效果,我错在哪了?望大侠们多多帮助,小生不胜感激!

土星的山羊的主页 土星的山羊 | 初学一级 | 园豆:191
提问于:2013-09-24 17:17
< >
分享
最佳答案
0

 更新语句有问题:标准格式如下:

 update  _tableEmployee  set  _tableEmployee.InitialSeniority = tableExcel.InitialSeniority 
  from _tableExcel where _tableEmployee.EmployeeId= _tableExcel.EmployeeId

你拼接的SQL 太雷人了。 实在不行你就用StringBuilder拼接! 可读性也好点,光看你update语句看了半天。

而且from前面为什么还有' ' 单引号? 你用的什么数据库?  

  

收获园豆:10
何以解忧唯有撸码 | 初学一级 |园豆:37 | 2013-09-24 18:20

 sql,单引号是变量的插入。的确,拼接的很雷人,我会注意的。_tableEmployee 和_tableExcel 两个是在内存里面的,

update  _tableEmployee  set  _tableEmployee.InitialSeniority = _tableExcel.InitialSeniority  from _tableExcel where _tableEmployee.EmployeeId= _tableExcel.EmployeeId,其中_tableEmployee 和_tableExcel都是变量,两张表都在内存。

有几个问题:

1. 如何用StringBuilder拼接 上面的语句,才能操作内存里的两张表呢?

2. 如果语句都正确,我下面的操作正确吗?!很是疑问。。

非常感谢。。。

土星的山羊 | 园豆:191 (初学一级) | 2013-09-24 20:41

@鹭起欣河: 我知道都是变量,你可以用我上面写的sql语句,替换你代码里面的SQL语句,也可以按照你上面那样拼接,我只是给你提供了个更新脚本木板。

stringbuilder拼接:

StringBuilder sb = new StringBuilder();

sb.Append("update ");

sb.Append(_tableEmployee );

sb.Append(" set ")

..

..

何以解忧唯有撸码 | 园豆:37 (初学一级) | 2013-09-24 21:20
其他回答(2)
0

你要用excel里的数据更新数据库里的数据?

  1. 从excel中查询数据填充到dataset中
  2. 把dataset中的每行数据的状态修改为updated
  3. 执行adapter.Update(dataset)【adapter连接的是数据库】

这么操作的前提是excel和数据库里的数据能对应上。你写的代码没太看明白

会长 | 园豆:12401 (专家六级) | 2013-09-24 17:31

是这样的,一个DataSet里装的是Excel里的数据,一个DataSet里装的是Employee表所有的数据,我现在需要将_tableExcel里的数据根据EmployeeID匹配批量更新到Employee表的InitialSeniority 字段中,我该如何实现呢?

内存里的两张表,在_tableEmployee表的基础上更新,然后再adapter.Update(dataset)?!

支持(0) 反对(0) 土星的山羊 | 园豆:191 (初学一级) | 2013-09-24 20:47
0

写的很复杂呢,推荐看下SqlBulkCopy,高速导入,操作简单,支持多种数据源。

幻天芒 | 园豆:37175 (高人七级) | 2013-09-25 00:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册