2016-04-24 33 views
-1

我想將profile_tb的許多記錄複製到Client_tb而不重複。 這段代碼在sql server上運行的很好,但我不知道下面的代碼被添加到asp.net中的客戶端頁面加載。如何在頁面加載asp.net中運行命令sql插入代碼?

SQL命令:

iNSERT iNTO Client (ProfileID) 
Select Distinct ProfileId From Profile Where Not Exists (Select 1 From 
Client where Profile.ProfileID = Client.ProfileID) 

plz幫助我!

回答

0

查看SqlConnection,SqlCommand類。

using (SqlConnection cn = new SqlConnection("your connection string")) { 
    SqlCommand cmd = new SqlCommand("INSERT iNTO Client (ProfileID) Select Distinct ProfileId From Profile Where Not Exists (Select 1 From Client where Profile.ProfileID = Client.ProfileID)", cn); 

    cn.Open(); 
    cmd.ExecuteNonQuery(); 
    cn.Close(); } 

我建議您查看SqlCommand,但是如果這是網站代碼,請使用參數方法來抵禦SQL注入攻擊。