用oledb方式连接excel后 想更新一列 update语句应该怎么写?

解决方案 »

  1.   

    myconn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+arr_str_xlsfiles[j]+";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
                        myconn.Open();
                        mycomm.Connection = myconn;
                        mycomm.CommandText = "update "+mysheet+" set flag=1 where message=\"CM SERVICE ACCEPT\"";
                        mycomm.ExecuteNonQuery();
    以上是代码 请高手看看
      

  2.   

    更新的语句写好了 可是运行后显示"操作必须使用一个可更新的查询" 我的是控制台应用程序 excel表所在的文件夹的只读属性总是取消不掉 哪位高人能帮帮我啊
      

  3.   

    你把excel文件放到根目录下,试试呗!
      

  4.   

    需要增添如下代码:1、定义:
       private OleDbCommandBuilder oleCB=null;  //与定义OleDbConnection放在一起2、初始化:
       oleCB= new SqlCommandBuilder(myconn);   //与初始化OleDbConnection放在一起现在你的代码就可以正常运行了,试试。
      

  5.   

    to:sdl2005lyx()
    commandbuilder的参数是dataadapter不是connection 而且定义的是oledb 初始化用sql 这也行?
      

  6.   

    对,是写错了,修改如下:(我原来用的是SQL SERVER)
    1、定义:
       private string strSql = "Select * from mysheet";
       private olelDataAdapter oleDA=null;
       private OleDbCommandBuilder oleCB=null;  //与定义OleDbConnection放在一起2、初始化:
       oleDA= new SqlDataAdapter(strSql, myconn);
       oleCB= new SqlCommandBuilder(oleDA);   //与初始化OleDbConnection放在一起
      

  7.   

    我把源码都贴出来,你看可能更方便写:
            private SqlConnection sqlCn=null;
            private SqlDataAdapter sqlDA=null;
            private string m_strCn = "Data Source=(local);Initial Catalog=LineMonitor;"
            + "Integrated Security=SSPI;";
            private string strSql = "Select * from UserManager";        private SqlCommandBuilder sqlCB = null;
            public UserForm()
            {
                InitializeComponent();            sqlCn = new SqlConnection(m_strCn);
                sqlDA = new SqlDataAdapter(strSql, sqlCn);
                sqlCB = new SqlCommandBuilder(sqlDA);
            }        private void Del_Click(object sender, EventArgs e)
            {
                string cmdText = string.Format("delete from UserManager where UserID={0:D}",Int32.Parse(UserNo.Text));
                SqlCommand cmd = new SqlCommand(cmdText, sqlCn);
                try
                {
                    sqlCn.Open();
                    int num = cmd.ExecuteNonQuery();  //首先HarmStat清空表
                    sqlCn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    sqlCn.Close();
                }
                cmd.Dispose();
            }
      

  8.   

    to:sdl2005lyx() 
    不是winform 是控制台 我试过了 还是显示“操作必须使用一个可更新的查询”
      

  9.   

    晕,这与FORM/控制台,没关系,我只是用FORM来举例罢了!!!用两种方式检查你的错误:1、有断点调试:
       将CommandText实际字符串放到查询分析器里执行,看看是不是有正确结果。(需要包excel文件倒到Acess或SQL Server)2、用ADO:
       这点我就不多说了
      

  10.   

    还有一点
    在执行更新的时候要关闭对应的Excel文档,否则异常!
      

  11.   

    to:sdl2005lyx()
    你连接的是sql数据库 不是excel表 用sql数据库我试过是可以的 但是excel就不行(不把excel导入access或sql 直接oledb连接)
      

  12.   

    其实我是想用update语句来更新excel的某一列 类似:update table set a=1 where b=2  这样的操作用单元格的方式也可以做 但是速度很慢 也不能导入数据库 所有才想用oledb连接的方式来做  没想到这么复杂  我看我还是用单元格的方式做吧 非常感谢sdl2005lyx()及其他人的解答 谢谢