C#加mysql

来源:互联网 发布:淘宝小石头运动添柏岚 编辑:程序博客网 时间:2024/06/05 04:17

在网上看了很多都是把所有数据拿回来,设置个数据桥,就在数据view展示了。 哎,想要个和Navicat一样的,能写语句的啊!

直接贴!


  // 获得数据库名称              
            string KuName = textBox1.Text.ToString();


            //获取语句
            string sql = richTextBox1.Text;


            //读链接          
            string myconn = string.Empty;


            if (comboBox1.Text == "读链接")
            {        
                myconn = *****
            }

            else
            {
                myconn = s****
            }


            //写链接
            conn = new MySqlConnection(myconn);


            //存储文件位置
            string path = string.Format(@"F:\{0}{1}.txt", KuName, DateTime.Now.ToString("yyyyMMddHHmmss"));


            FileStream fs1 = new FileStream(path, FileMode.Append, FileAccess.Write);//创建写入文件 


            StreamWriter sw = new StreamWriter(fs1);


            try
            {
                //打开连接
                conn.Open();


                // 设置数据桥              
                dataAdapter = new MySqlDataAdapter("USE " + KuName +";\r\n"+ sql, conn);


                // DataSet              
                sqlCmdBuilder = new MySqlCommandBuilder(dataAdapter);


                // 数据
                DataSet ds = new DataSet().;


                string[] arr = new string[9999999];


                int n = 0;


                foreach (DataRow dr in ds.Tables[0].Rows)
                {


                    for (int i = 0; i < dr.ItemArray.Length; i++)
                    {


                        arr[n] += dr.ItemArray[i].ToString() + "  ";


                    }


                    n = n + 1;


                }


                for (int i = 0; i < arr.Length; i++)


                {


                    sw.WriteLine(arr[i]);
                    sw.Flush();


                }


                sw.Close();
                fs1.Close();


            }


            catch (Exception ex)
            {


                sw.WriteLine(ex.ToString());
                sw.Flush();
                sw.Close();
                fs1.Close();


            }
            finally
            {
                conn.Close();
            }         

原创粉丝点击