簡體   English   中英

從Asp.NET將行復制到SQL Server中的另一行

[英]Copying row to another one in SQL Server from Asp.NET

假設存在一個僅包含ID, Name, CompanyRealID

我想將一行復制到另一行。 例如:

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)

我搜索此主題並找到了這一點: 從另一行復制值

但我認為情況不同。

還是有什么方法可以縮短這個過程?

不久:

我想從asp.net在sql中將一行復制到另一行。

感謝幫助

看起來您幾乎擁有所有代碼,那么為什么需要快捷方式? 我不知道快捷方式,如果數據像您的例子一樣瑣碎,誰在乎! 只需讀出並寫...

我不確定我是否正確理解了這個問題,但是如果您想將一個表中的行逐行復制到另一張表中,可以使用INSERT INTO,

如果這只是一行的副本,並且Id是自動遞增的,也許您可​​以僅作為SQL來執行此操作?

INSERT INTO UserProfile (Name, Company, RealID) SELECT Name, Company, RealID FROM UserProfile WHERE ID = X

如果存在選擇更改的值,也可以替換為:

INSERT INTO UserProfile (Name, Company, RealID) SELECT 'New Name', Company, RealID FROM UserProfile WHERE ID = X

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM