如何使用C#将数据从1个表导入到另一个表 [英] How to import data from 1 table to another using C#
问题描述
我需要帮助编写代码来完成数据传输。请在答案中包含example / psuedo代码。
现状:
我有一个我已经建立的网络应用程序。我需要获取excel工作表数据,我已将其导入SQL Server Mgmt Studio,并将其移动到另一个表中。我有一个网页,其中1个按钮负责导入事件。一般情况下,这并不难,只是我导入的表中有1列是名称字段,我需要将其转换为数字(id)字段。
到目前为止我有什么:
我添加了评论解释我的代码片段以及指示我需要帮助编写代码的地方。
I need help with writing the code to accomplish a data transfer. Please include example/psuedo code in your answer.
Current Situation:
I have a web application I've built. I need to take an excel sheets data, which I've imported into SQL Server Mgmt Studio, and move it into another table. I have one webpage with 1 button responsible for the import event. In general this isn't that hard, it's just that I have 1 column in my imported table that is a name field and I need to convert it to a number (id) field.
What I have so far:
I've added comments to explain pieces of my code as well as to indicate where I need help writing the code.
protected void uxImport_Click(object sender, EventArgs e)
{
var accountTrackerImportSVC = new AccountTrackerImportSVC();
var dtInfo = GetContent(); // dtInfo is = to my Imported table
// View connectiontype by description
// Pass ConnectionType
// if the connectiontype description exists grab the i.d
// if not then create an id, either way insert an entry
foreach (DataRow drItem in dtInfo.Rows)
{
var connectionType = drItem["ConnectionTypeDesc"].ToString();// connectionType is being looped by foreach statement, I'm using var to view each connectiontype in my import table.
{
AccountTrackerImportDAL.ViewConnectonTypeByDesc(connectionType);
if (connectionType == null)
{
//var result = AccountTrackerImportDAL.ImportIntoAccountEntry();
}
else
{
}
// I need to split name.
// first name match if yes return Worker Num
}
//Check results
if(result.Successful)
{
uxInfoMsg.DisplayMessage(result.Message, InfoMessage. InfoMessageType.Success);
}
else
{
}
// Hide progress indicator
Master.HideProgressIndicator();
}
关于如何处理此问题的任何想法。我还是C#的新手所以我会尽力回答你的问题。
Any ideas on how to approach this. I'm still new to C# so I'll do my best to answer your questions.
推荐答案
SQL伪代码:
SQL Pseudo code :
insert into SecondTableName
select Column1,Column2
from FirstTableName
where YourConditionHere
C#代码:
try
{
con.open();
cmd=new sqlcommand("insert into SecondTableName select Column1,Column2
from FirstTableName where YourConditionHere",conn);
cmd.executenonquery();
con.close();
}
catch(exception ex)
{
}
finally
{
con.close();
}
用您的实时查询替换伪代码查询
Replace the Pseudo code Query with your real time query
这篇关于如何使用C#将数据从1个表导入到另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!