假设使用SQL Server 2008+,则可以使用表值参数来执行此操作。首先在SQL Server中创建一个表类型:
CREATE TYPE dbo.HobbiesTVP AS TABLE( HobbyID INT PRIMARY KEY, HobbyName NVARCHAr(50), HobbyTypeID INT);
然后,您的存储过程将显示:
@Hobbies dbo.HobbiesTVP READONLY
在C#中(对不起,我不了解vb.net等效项)它将如下所示(但是,如果您只有一个UserID,则不必成为集合的一部分,对吗?):
// as Steve pointed out, you may need to have your hobbies in a DataTable.DataTable HobbyDataTable = new DataTable();HobbyDataTable.Columns.Add(new DataColumn("HobbyID"));HobbyDataTable.Columns.Add(new DataColumn("HobbyName"));HobbyDataTable.Columns.Add(new DataColumn("HobbyTypeID"));// loop through objHobbyCollection and add the values to the DataTable,// or just populate this DataTable in the first placeusing (connObject){ SqlCommand cmd = new SqlCommand("dbo.User_Update", connObject); cmd.CommandType = CommandType.StoredProcedure; // other params, e.g. @UserID SqlParameter tvparam = cmd.Parameters.AddWithValue("@Hobbies", HobbyDataTable); tvparam.SqlDbType = SqlDbType.Structured; // ...presumably ExecuteNonQuery()}


