小生初学C# 目前学习C#数据库方面的知识,在用Access创建一个数据库后想连接并进行操作,代码如下,可是下面进行操作是提示错误:
D:\VSC#program\ADOWindowsForms\ADOWindowsForms\Form1.cs(85,34): 错误 CS0103: 当前上下文中不存在名称“myConn”
D:\VSC#program\ADOWindowsForms\ADOWindowsForms\Form1.cs(106,34): 错误 CS0103: 当前上下文中不存在名称“myConn”
D:\VSC#program\ADOWindowsForms\ADOWindowsForms\Form1.cs(117,34): 错误 CS0103: 当前上下文中不存在名称“myConn”
D:\VSC#program\ADOWindowsForms\ADOWindowsForms\Form1.cs(132,34): 错误 CS0103: 当前上下文中不存在名称“myConn”请问这是怎么回事啊?
代码:
private void button1_Click(object sender, EventArgs e)
        {
            string strPath;
            strPath = txtDBPath.Text;
            OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+strPath );
            myConn.Open ();
        }        private void button5_Click(object sender, EventArgs e)
        {
            if (txtCustomerID.Text.Length == 0)
            {
                MessageBox.Show("请输入与查询的员工编号!!");
                return;
            }
            string strCustomerID = txtCustomerID.Text;
            string strSQL = "SELECT CustomerID,CustomerName,Address,Phone " + "From Customers " +
                "WHERE CustomerID='" + strCustomerID + "'";            OleDbCommand myCmd = myConn.CreateCommand();
            myCmd.CommandText = strSQL;
            OleDbDataReader myDr = myCmd.ExecuteReader();
            myDr.Read();            txtCustomerName.Text = myDr["CustomerName"].ToString();
            txtDBPath.Text = myDr["Address"].ToString();
            txtTel.Text = myDr["Phone"].ToString();            myDr.Close();
        }        private void button2_Click(object sender, EventArgs e)
        {
            string strCustomerID = txtCustomerID.Text ;
            string strCustomerName = txtCustomerName.Text ;
            string strAddress = txtAddress.Text ;
            string strPhone = txtTel.Text;            string strSQL = "INSERT INTO Customers " + "(CustomerID,CustomerName,Address,Phone) " + "VALUES " +
                "(" + strCustomerID + ",'" + strCustomerName + ",'" + strAddress + ",'" + strPhone + "')";
            OleDbCommand myCmd = myConn.CreateCommand;
            myCmd.CommandText = strSQL;
            myCmd.ExecuteNonQuery();
            MessageBox.Show("增加一笔数据完毕!!");
        }        private void button3_Click(object sender, EventArgs e)
        {
            string strCustomerID = txtCustomerID.Text ;
            string strSQL = "DELETE FROM Customers WHERE CustomerID =" + strCustomerID +"";            OleDbCommand myCmd = myConn.CreateCommand();
            myCmd.CommandText = strSQL;
            myCmd.ExecuteNonQuery();
            MessageBox.Show("删除客户编号 = !" + strCustomerID + " 的数据一笔!");
        }        private void button4_Click(object sender, EventArgs e)
        {
            string strCustomerID = txtCustomerID.Text;
            string strCustomerName = txtCustomerName.Text;
            string strAddress = txtAddress.Text;
            string strPhone = txtTel.Text;            string strSQL = "UPDATE Customers SET "+"CustomerName= '"+strCustomerName +"',"+
                "Address= '"+strAddress +"',"+"Phone= '"+strPhone +"'"+"WHERE CustomerID ="+strCustomerID +"";
            OleDbCommand myCmd = myConn.CreateCommand();
            myCmd.CommandText = strSQL;
            myCmd.ExecuteNonQuery();
            MessageBox.Show("更新客户编号"+strCustomerID +"的数据一笔!!");
        }