C#连接数据库

来源:互联网 发布:mac虚拟机安装win7不了 编辑:程序博客网 时间:2024/06/05 07:41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySQLDriverCS;
using System.Windows.Forms;
namespace 清洁生产报告辅助软件
{
    class Mysql
    {
        public string[] str1;
        public string[] str5;
        public Mysql(string str)
        {
            //得到文本框内容进行检索
            MySQLConnection conn = null;


            conn = new MySQLConnection(new MySQLConnectionString("localhost", "test", "root", "cao564335").AsString);


            conn.Open();
            String sqlInsert1 = "SELECT COUNT(new1) FROM t_table3";


            // String sqlInsert = "insert into t_table3 values ('1','25','E:\\\\\\\\保存的文件\\\\\\\\3.txt')";




            MySQLCommand commn2 = new MySQLCommand(sqlInsert1, conn);
            commn2.ExecuteScalar();
            string str2 = commn2.ExecuteScalar().ToString();
            int v1 = Convert.ToInt32(str2);
            this.str1 = new string[v1];
            this.str5 = new string[v1];
            MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
            commn.ExecuteNonQuery();
            string sd = "select new3,new2  from  t_table3 where new3  like'" + "%" + str + "%'";//进行查找
            MySQLCommand cmd = new MySQLCommand(sd, conn);//


            MySQLDataReader dr = cmd.ExecuteReaderEx();
            int i = 0;
            while (dr.Read())
            {
                this.str1[i] = dr[0].ToString();
                this.str5[i] = dr[1].ToString();
                i++;




            }
            
          // MessageBox.Show("第一个界面" + str5[0]);
          //  MessageBox.Show("第er个界面" + str1[0]);
            dr.Close();
            
            conn.Close();
        }
    }
}
原创粉丝点击