如何将dataTable转换为脚本并使用C#将其保存到表中 [英] How to convert dataTable into script and save it in to table using C#

查看:65
本文介绍了如何将dataTable转换为脚本并使用C#将其保存到表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



i需要你的帮助

i有一个查询我是从客户端数据库中选择一些数据并希望保存在内部数据库。

客户端数据库是mysql,我们的数据库是mssql所以单个查询我想得到选择脚本并插入脚本两个脚本都要保存在mssql表上单个保存按钮点击发明一些进程完成50时间并保存在表格中的所有脚本。请让我知道,如果有人知道我需要优先保险。

Dear All,

i need your help
i have one query i was select some data from client database and want to save in internal database.
client database is mysql and our database is mssql so on single query i want to get select script and insert script both script want to save in mssql table on single save button click invent some process done 50 time and save all script in table.So pls let me know if some one know it i need heelp on priority.

推荐答案

protected void btnDatabaseMigration_Click(object sender, EventArgs e)
    {
         string str = string.Empty;
        DataSet ds1 = new DataSet();
        DataView dv;
        
        foreach (ListItem li in TableCheckBoxList.Items)
        {
            if (li.Selected)
            {
                MySqlConnection myconn3;
                string abc = "";
                abc = "Data Source=localhost;Initial Catalog=" + lblMySqlDB.Text + ";user id=root;password=root";
                myconn3 = new MySqlConnection(abc);
                myconn3.Open();
                str = li.ToString();
                string query = "Select " + str + " from " + lblMySqlDB.Text + "." + Label5.Text + "";
                MySqlDataAdapter mydata = new MySqlDataAdapter(query, myconn3);
                DataTable dTable = new DataTable();
                mydata.Fill(ds1, str);
                dv=ds1.Tables[0].DefaultView;
                DataTable table = ds1.Tables[0];
                dv.Sort = "" + str + " DESC";
                myconn3.Close();
               
            }
           

          //  int a = 0;
            foreach (DataTable dt in ds1.Tables)
            { 
                
            }
            if (ds1.Tables.Count > 0)
            {
                string strForSqlServer = string.Empty;
                DataSet dsForSqlServer = new DataSet();
                foreach (ListItem listOFSQL in CheckBoxIntellectTableData.Items)
                {
                    if (listOFSQL.Selected)
                    {
                        SqlConnection sqlconForMigration;
                        // string abc = "";
                        //  abc = "Data Source=localhost;Initial Catalog=" + lblSqlDatabase.Text + ";user id=root;password=root";
                        foreach (DataRow dr in ds1.Tables[0].Rows)
                        {
                            //for (int i = 0; i < dr.Table.Columns.Count; i++)
                            //{
                            sqlconForMigration = new SqlConnection(strIn);
                            sqlconForMigration.Open();
                            strForSqlServer = listOFSQL.ToString();
                            string query = "insert into " + lblSqlTableName.Text + "(" + strForSqlServer + ") values ('" + dr[0].ToString() + "')";
                            SqlCommand cmd = new SqlCommand(query, sqlconForMigration);
                            cmd.ExecuteNonQuery();
                            sqlconForMigration.Close();
                            //}
                        }

                    }
                }
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Record Added In MSSQL Table')", true);
            }

               // a++;
                  
            }
      

        }


这篇关于如何将dataTable转换为脚本并使用C#将其保存到表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆