[英]C# SQL Data Mapping into Visual Studio
I have a massive amount of data columns in a certain table an now need to map these into C# Visual Studio. 我在某个表中有大量数据列,现在需要将它们映射到C#Visual Studio中。 Is there an easy way of doing this or is it still a laborious manual task. 有没有简单的方法可以做到这一点,还是仍然很费力的手动任务。 Here is an example of what I mean: 这是我的意思的示例:
string query = @"UPDATE Buyer
SET DealID = @DealID,Title=@Title,FirstName=@FirstName,LastName=@LastName,
IDNo=@IDNo,Address=@Address,POAddress=@POAddress,HomeTel=@HomeTel,
WorkTel=@WorkTel,Cell=@Cell,Fax=@Fax,Email=@Email,
MarritalStatus=@MarritalStatus,SpouseFirstName=@SpouseFirstName,
SpouseLastName=@SpouseLastName,SpouseWorkTel=@SpouseWorkTel,
SpouseCell=@SpouseCell,SpouseFax=@SpouseFax,Notes=@Notes,
id=@id,DealID=@DealID WHERE IDNo = @IDNo";
SqlCommand cm = new SqlCommand(query, cn);
string id = iDNoTextBox.Text;
cm.Parameters.AddWithValue("@DealID", txtDealNo.Text);
cm.Parameters.AddWithValue("@id", iDNoTextBox.Text);
cm.Parameters.AddWithValue("@Title", titleComboBox.Text);
cm.Parameters.AddWithValue("@FirstName", firstNameTextBox.Text);
You have some choices to do it in simple way 您可以选择一些简单的方法
First : Use ORMs such EntityFramework
, for example you can use dapper-dot-net
, sample code: 首先:使用ORM这样的EntityFramework
,例如,您可以使用dapper-dot-net
示例代码:
var dog= connection.Query("select Age = @Age, Id = @Id", new {Age = (int?)null,Id=guid});
Insert example: 插入示例:
connection.Execute(@"insert MyTable(colA, colB) values (@a, @b)",
new[] { new { a=1, b=1 }, new { a=2, b=2 }, new { a=3, b=3 } }
).IsEqualTo(3); // 3 rows inserted: "1,1", "2,2" and "3,3"
Second: Use Array of type SqlParameter
and insert that into SqlCommand
第二:使用SqlParameter
类型的数组并将其插入SqlCommand
SqlCommand Comm = new SqlCommand("Command text", new SqlConnection("Connection String");
SqlParameter[] param = {new SqlParameter("@Name","Value"),
new SqlParameter("@Name","Value"),
........
};
Comm.Parameters.AddRange(param);
Third: Or write a good Sql Assistant
and make a generic mapper for object-to-relational
and vice versa. 第三:或者编写一个好的Sql Assistant
并为object-to-relational
创建通用映射器,反之亦然。 then you can produce your parameters at run-time and dynamically without hard coded. 那么您可以在运行时动态地生成参数,而无需进行硬编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.