将表更新为excel

时间:2014-06-06 14:28:23

标签: c# excel datagridview

我正在尝试更新"放在dataGridView上的单元格中的数据。我将数据从excel文件导入dataGridView。使用这种方法;

public static void updateTable1(string myNameRange, string mySET, string myWHERE)
    {
        string sql = null;
        sql = "UPDATE " + myNameRange + " SET " + mySET + "' WHERE " + myWHERE + "'";
        //sql = "INSERT INTO" + myNameRange + " SET " + mySET + "'";
        myCommand.CommandText = sql;
        myCommand.ExecuteNonQuery();


    }

如果我们有列标题NAME和AGE,如果我输入一个名字杰克和迈克10岁和20岁的表,如果我将杰克斯的年龄从10改为30,这是有效的。代码说......其中列NAME = Jack然后将列AGE设置为30.这再次起作用。问题是,如果我想更改NAME和AGE ...那么将Jack更改为Matt,并将Mikes age更改为40.或者将另一个姓名和年龄添加到我的表中。这种方式不会工作。是否有一种简单的方法来更新整个表格并进行任何更改?

2 个答案:

答案 0 :(得分:0)

假设您有3个文本框,其中用户填写NAME和AGE(以及旧名称):

创建一个将检查的方法:

  1. 如何填写文本框
  2. 如果在DB
  3. 中存在最后一个文本框中输入的名称

    假设检查是这种方法:

    public static void updateTable1(string myNameRange, string mySET, string myWHERE)
        {
            if (Check()==0)/// Suppose 0 means all textboxes filled and name2 exists in DB
           {      
            string sql = "UPDATE myTable SET  NAME=@name , AGE=@age  WHERE Name=@name2";
    
            myCommand.CommandText = sql;
            myCommand.Parameters.AddWithValue("@name", textBox1.text);
            myCommand.Parameters.AddWithValue("@age", textBox2.text);
            myCommand.Parameters.AddWithValue("@name2", textBox3.text);
            myCommand.ExecuteNonQuery();
            }
            else if  (Check()==1)/// Suppose 1 means only name filled and name2 exists in DB
            {
              string sql = "UPDATE myTable SET  NAME=@name  WHERE Name=@name2";      
            myCommand.CommandText = sql;
            myCommand.Parameters.AddWithValue("@name", textBox1.text);       
            myCommand.Parameters.AddWithValue("@name2", textBox3.text);
            myCommand.ExecuteNonQuery();
            }
            ....
            else /// (name2 not in DB)
            {
             sql = "INSERT INTO" + myNameRange + " SET " + mySET + "'";
              ///.......
            }
        }
    

答案 1 :(得分:0)

        Create stored procedure like the sample one below, here you only pass all the values that were changed, then the stored procedure will take care of the update. 

        public static void updateTable1(string? NameValue, int? AgeValue, int uniqueValue)
        {
            try 
            {           
                SqlCommand myCommand = new SqlCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "spUpdate_ProcName";
                myCommand.Parameters.AddWithValue("@Name", NameValue);
                myCommand.Parameters.AddWithValue("@Age", AgeValue);
                myCommand.Parameters.AddWithValue("@UniqueColumn", uniqueValue);
                myCommand.ExecuteNonQuery();
            }
            catch (Exception)
            {       
                throw;
            }


        }
        //To be done in SQL
        CREATE PROCEDURE upProcName
        (
            @Name varchar(100) = NULL,
            @Age int = NULL,
            @UniqueColumn int -- unique column or primary key
        )
        AS
        BEGIN
            SET NOCOUNT ON;

            UPDATE yourTableName
            SET Name = COALESCE(@Name, Name),
                Age = COALESCE(@Age, Age)
            WHERE UniqueColumn = @UniqueColumn
        END