问题描述
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingMySql.Data.MySqlClient;namespaceMySqlDemo{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidConn_Btn_Click(objectsender,EventArgse){MyDatadata=newMyData();reader=data.myReader();while(reader.Read()){ListViewItemitem=newListViewItem();item.Text=reader.GetString(0);listView1.Items.Add(item);}}}publicclassMyData{//Mysql版本6.1MySql.dll版本号5.1MySqlCommandcomm;stringconnstr="server=localhost;userid=root;password=yqh2648;database=birthcontrol";stringselectstr="selectv_namefromvillageinfo";MySqlConnectionconn;MySqlDataReaderreader;publicMySqlDataReadermyReader()\如果将这个函数放入到Form1类调用则不会造成数据丢失,但是我的工程是DLL类库,所以放入主窗体是不可能的!!!{conn=newMySqlConnection(connstr);comm=newMySqlCommand(selectstr);conn.Open();comm.Connection=conn;reader=comm.ExecuteReader();while(reader.Read()){returnreader;}returnnull;}}}
解决方案
解决方案二:
publicMySqlDataReadermyReader()\如果将这个函数放入到Form1类调用则不会造成数据丢失,但是我的工程是DLL类库,所以放入主窗体是不可能的!!!{conn=newMySqlConnection(connstr);comm=newMySqlCommand(selectstr);conn.Open();comm.Connection=conn;reader=comm.ExecuteReader();if(reader.HasRows){returnreader;}returnnull;}