//请帮忙完成代码或修改代码 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.OleDb; namespace WindowsFormsApplication1 

public partial class Form1 : Form 

    public Form1() 
    { 
        InitializeComponent(); 
        } 
            class MySql 
            { 
                public static string Sql(string app) 
                { 
                    OleDbConnection Cnn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\MoBin\桌面\1.accdb"); 
                    OleDbCommand Cmd = new OleDbCommand(app, Cnn); 
                    Cnn.Open(); 
                    OleDbDataReader Dr = Cmd.ExecuteReader(); 
                } 
            } 
            private void button1_Click(object sender, EventArgs e) 
            { 
                string s = "select * from da"; 
                while() //这里本来是Dr.Read()的,可是我把连接数据库的代码写在类里了,这里因该怎么写. 
            { 
                listBox1.Items.Add(MySql.Sql(s,0)); 
            } 
        } 
    } 
} //把内容读取到LISTBOX1之后,怎么更改数据库字段的内容.(就是修改数据)//如果MYSQL那个类有写错的地方还望高手们更正.
//请给出详细代码

解决方案 »

  1.   

    以前做过的例子 从数据库中读取 学号 姓名  成绩到 列表框中的  
    OleDbConnection conn = new OleDbConnection(); 
             
                string strconn; 
                strconn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb"; 
                conn.ConnectionString = strconn; 
                try 
                { 
                    conn.Open(); 
                    OleDbCommand command = new OleDbCommand(); 
                    command.Connection = conn; 
                    command.CommandText = "SELECT xh AS 学号,xm AS 姓名,cj AS 成绩 FROM table1 "; 
                    OleDbDataReader dr; 
                    dr = command.ExecuteReader(); 
                    while (dr.Read()) 
                    { 
                        this.listBox1.Items.Add(dr.GetString(0)); 
                        this.listBox2.Items.Add(dr.GetString(1)); 
                        this.listBox3.Items.Add(dr.GetValue(2).ToString()); 
                    } 
                } 
                catch (Exception oleE) 
                { 
                    MessageBox.Show(oleE.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                } 
                finally 
                { 
                    conn.Close(); 
                } 
      

  2.   

    string StrCon="";
    string strSQL="SELECT * FROM table1"; 
    OleDbConnection MyConnection=new OleDbConnection(StrCon); 
    OleDbCommand myCommand=new OleDbCommand(strSQL,MyConnection);  
    OleDbDataReader myDataReader=null; 
    myCommand.Connection.Open();  
    myDataReader=myCommand.ExecuteReader(); 
    myDataReader.Read(); 
    Response.Write("dnews="+myDataReader["aa"].ToString()); 
    myCommand.Connection.Close();
      

  3.   

     public static OleDbDataReader ExecuteReader(string strSQL)    
            {    
                OleDbConnection connection = new OleDbConnection(connectionString);    
                OleDbCommand cmd = new OleDbCommand(strSQL, connection);    
                try  
                {    
                    connection.Open();    
                    OleDbDataReader myReader = cmd.ExecuteReader();    
                    return myReader;    
                }    
                catch (System.Data.OleDb.OleDbException e)    
                {    
                    throw new Exception(e.Message);    
                }    
                
            }     
      

  4.   

    access操作类参考
      

  5.   

        System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection(@"Data Source=PC-200903131045\HUGUO;Initial Catalog=F:\我的单子\200904\项目\ZYT\APP_DATA\BJYZT.MDF;User ID=sa;Password=123456");
              System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter("select ID, Title, ParentID from [BJProductCategory] where ParentID = @ID", cn);
            da.SelectCommand.Parameters.AddWithValue("@ID", Convert.ToInt32(ID));
            DataSet ds = new DataSet();
            cn.Open();
            da.Fill(ds);
            cn.Close();
            return ds.Tables[0];
      

  6.   

    个人的一点建议:新建一个数据库连接类,该类里包括了数据库连接,增、删、改操作函数的接口。当然,数据库连接字符串要写到配置文件(webconfig)里,这样不至于数据库存放地址改变了而重新编译工程。在自己的业务类里操作数据库,直接调用数据库连接类就可以了。
    比如举个例子:
    #region 执行一个查询,返回受影响的行数,结果装载到内置对象中
    public int openRset(string Query)
    {
    int Result;
    Fds.Clear(); 
    comm.CommandText=Query;
    Result=Ad.Fill(Fds);
    return Result;
    }
    #endregion