Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,
My below code is working fine i am able to select records from one table to second table
But my query is if user selected same table or same column than want to updated only new records how can i do pls help me
C#
private void btnSave_Click(object sender, EventArgs e)
        {

            string str = string.Empty;
            DataSet ds1 = new DataSet();
            DataView dv;
            string clientdata="";

            foreach (var clientItem  in checkedListBoxClientTableField.CheckedItems)
            {
                if (checkedListBoxClientTableField.SelectedIndex > -1)
                {
                    foreach(DataRowView dataFromClient in checkedListBoxClientTableField.CheckedItems)
                    {
                        clientdata=dataFromClient[0].ToString();
                    }

                    MySqlConnection myconn3;
                    string SelectClientData = "Data Source="+lblSaveSeverIpClient.Text +";Initial Catalog=" + lblSqlDatabaseClient.Text + ";user id="+lblSaveUserNameClient.Text +";password="+ lblSavePasswordClient.Text +"";
                    myconn3 = new MySqlConnection(SelectClientData);
                    myconn3.Open();
                    str = clientdata.ToString();
                    string query = "Select " + str + " from " + lblSqlDatabaseClient.Text + "." + lblMySqlTableNameClient.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();
                    string strInt="";
                    foreach (var listOFSQLIntelect in checkedListBoxIntTable.CheckedItems)
                    {
                        if (checkedListBoxIntTable.SelectedIndex > -1)
                        {
                              foreach(DataRowView DriInternal in checkedListBoxIntTable.CheckedItems)
                              {
                                   strInt=DriInternal[0].ToString();
                              }
                              
                            SqlConnection sqlconForMigration;  
                             string SqlServerInsert = "Data Source=" + lblSaveSeverIpIntellect.Text + ";Initial Catalog=" + lblSqlDatabase.Text + ";user id=" + lblSaveUserNameIntellect.Text + ";password=" + lblSavePasswordIntellect.Text + "";
                           // string  SqlServerInsert = "Data Source=localhost;Initial Catalog=" + lblSqlDatabase.Text + ";user id=root;password=root";
                            foreach (DataRow dr in ds1.Tables[0].Rows)
                            {
                                
                                sqlconForMigration = new SqlConnection(SqlServerInsert);
                                sqlconForMigration.Open();
                                strForSqlServer = strInt.ToString();
                                string query = "insert into " + lblSqlTableName.Text + "(" + strForSqlServer + ") values ('" + dr[0].ToString() + "')";
                                SqlCommand cmd = new SqlCommand(query, sqlconForMigration);
                                cmd.ExecuteNonQuery();
                                sqlconForMigration.Close();
                                
                            }

                        }
                    }
                    string message = "Record Added In MSSQL Table";
                    string title = "Title";
                    MessageBox.Show(message);
                   
                      
                }

              

            }
            //string path = "D:\\Expences.xml";
            //XmlDocument doc = new XmlDocument();
            ////If there is no current file, then create a new one
            //if (!System.IO.File.Exists(path))
            //{
            //    //Create neccessary nodes
            //    XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            //    XmlComment comment = doc.CreateComment("This is an XML Generated File");
            //    doc.AppendChild(declaration);
            //    doc.AppendChild(comment);
            //}
            //else //If there is already a file
            //{
            //    doc.Load(path);
            //}

        }
Posted
Comments
[no name] 12-Feb-14 7:42am    
Check for the condition if the record already exists . what I do understand from your question - you are moving records form one table to other table.

solution 1. check for already exiting records .
solution 2. ensure you do have a primary key constraint which resolves the issue.

Regards,
Praveen Nelge

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900