假設有一個表只包含ID, Name, Company
和RealID
。從Asp.NET將行復制到SQL Server中的另一個行
我想複製一行到另一行。對於前:
ID Name Company RealID
1 Marry Company A null
2 Jane Company B null
3 Watson Company C null
4 Marry Company A 1
5 Watson Company C 3
我將通過C#編程語言做到這一點從asp.net。
我的問題是:
在c#下面應該做些什麼?
Class: UserProfile {ID, Name, Company, RealID}
代碼方面:
UserProfile userProfile = new UserProfile();
//I have userProfile.ID, userProfile.Name, userProfile.Company and userProfile.RealID
SqlConnection conn = new SqlConnection(sqlConnection);
SqlCommand cmd = new SqlCommand("Select * From UserProfile", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
//and with a loop, assignment to UserProfile members such as UserProfile.ID = dr[0][i]...
和SQL查詢後會
INSERT (Name, Company, RealID)
VALUES (UserProfile.Name, UserProfile.Company, UserProfile.RealID)
我搜索這個問題,並發現這一點: Copying values from another row
,但我覺得我的情況下,是不同的。
或者有什麼方法可以縮短這個過程嗎?
不久:
我想一列從SQL複製asp.net到另一個。
感謝您的幫助
完全以同意。在「插入其他表中的數據」部分中查找[此處](http://technet.microsoft.com/zh-cn/library/dd776381(SQL.105).aspx),示例a。它更簡單。 – aikixd 2012-07-31 20:53:34
我認爲這個例子非常清楚.. – 2012-08-01 00:25:45