blscorp
blscorp

Reputation: 3

How can I import data from 1st table to a 2nd table on the same database?

I can do this in SQL Server Management Studio, but when it comes to C#, I'm hopeless.

SQL Server Management Studio query:

INSERT INTO LoginDetails  
    SELECT Username, Password   
    FROM AccountLogin; 

The following code saves data on a local db:

con.Open();
string query = "INSERT INTO AccountLogin (Name,LastName,TelNo,Email,Password,RepeatPassword,Username,TypeofRegistration,JobPosition) VALUES ('" + txtname.Text + "','" + txtlastname.Text + "','" + txttelno.Text + "','" + txtemail.Text + "','" + txtaccpassword.Text + "','" + txtrepaccpassword.Text + "','" + txtaccusername.Text + "','" + TypeofREgistration.GetItemText(TypeofREgistration.SelectedItem) + "','" + JobPosition.GetItemText(JobPosition.SelectedItem) + "')";
SqlDataAdapter SDA = new SqlDataAdapter(query, con);
SDA.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("REGISTRATION COMPLETE!");

Now i want to write a query that at the same time ,the data that i first save to a table of the local db to be saved in the 2nd table of the same local db

Upvotes: 0

Views: 63

Answers (1)

Joe van de Bilt
Joe van de Bilt

Reputation: 275

If the databases are within the same SQL Serve instance, then you can do it like this. Also assuming you are using the default dbo schema.

INSERT INTO [Database1].[dbo].[LoginDetails]
SELECT Username, Password
FROM [Database2].[dbo].[AccountLogin]

Upvotes: 1

Related Questions