当前上下文中不存在变量和“可能错误的空语句”

时间:2017-07-01 16:01:40

标签: c# database ms-access datagrid

我已经编写了代码,它将打开与数据库的连接(conn),然后删除表中的所有记录。接下来是不会按预期编译或工作的部分,在当前上下文中不存在conn或连接,Ive在代码中的行中插入错误以及可能的空语句可能警告。失败的代码是我可以看到它上面的代码镜像编译和工作正常。代码如下。

{
        panel2.Visible = false;
        int i = dataGridView1.CurrentCell.RowIndex;
        i = dataGridView1.SelectedRows[0].Index;
        DataGridViewRow newDataRow = dataGridView1.Rows[i];
        dataGridView1.Rows[i].SetValues(label3.Text, "two", "Three", "Four");

       // string str = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False";




        String connStr, sql;
        connStr = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False");
        try
        {
            //Empty the table
            sql = "Delete from " + table;
            using (OleDbConnection conn = new OleDbConnection(connStr))
            {
                conn.Open();
                using (OleDbCommand cmd1 = new OleDbCommand(sql, conn))
                {
                    cmd1.ExecuteNonQuery();
                }
            }
        }
        catch (Exception ex)
        {
            SetCon.Text = SetCon.Text + "Error " + ex + "\n";
        }

            OleDbCommand cmd = new OleDbCommand();
            for (int ii = 0; ii < dataGridView1.Rows.Count; ii++)
            {
                DataGridViewRow dr= dataGridView1.Rows[ii];
                if (dr.Selected == true)
                {
                  string connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\Set.mdb;Persist Security Info=False";


                    try
                    {
                        int number = 0; //possible empty statment warning next line
                        using (OleDbConnection connection = new OleDbConnection(connetionString)) ;
                    {
                        sql = "UPDATE EnableOne SET Property=@var1, Pvalue=@var2  Pdefault=@var2 PType=@var2";
                        connection.Open(); //This fail's to compile connection does not exist in the current context
                        using (OleDbCommand cmd1 = new OleDbCommand(sql, connection))//This fail's to compile connection does not exist in the current context
                        {
                            number = dataGridView1.CurrentCell.RowIndex;
                            foreach (DataGridViewRow row in dataGridView1.SelectedRows)
                            {
                                cmd1.Parameters.Add(new OleDbParameter("@var1", row.Cells[0].Value.ToString()));
                                cmd1.Parameters.Add(new OleDbParameter("@var2", row.Cells[1].Value.ToString()));
                                cmd1.Parameters.Add(new OleDbParameter("@var3", row.Cells[2].Value.ToString()));
                                cmd1.Parameters.Add(new OleDbParameter("@var4", row.Cells[3].Value.ToString()));
                                cmd1.CommandText = sql;
                                cmd1.ExecuteNonQuery();
                            }

                        }
                    }
                    }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                }

            } }
    }

0 个答案:

没有答案