使用C#更新多个sql表中的同一列
场景:
我有一部手机,比较 ASP.NET 网站,该网站显示各种网络的交易。我们从网络收到的交易数据位于 Excel 表格中。我们将 Excel 工作表中的数据导入到数据库中每个网络的表中。现在,我们收到的数据对于每个网络来说并不一致,例如,一个网络可能将电话名称命名为“curve 8250”,另一个网络可能将其命名为“8250 curve”,另一个网络可能将其命名为“8250curve”。
现在,我们有另一个模块,允许用户查看所有网络上可用的特定手机的交易。为了使这个模块工作,我们需要做的是确保所有网络的电话名称都是一致的。
为此,我计划为网络管理员制作一个模块,用于显示所有网络表中的电话名称(可能在网格视图中),并且网站管理员可以编辑电话名称以使它们一致。从所有表中检索不同的列名很容易,就这样完成了。
问题: 现在,真正的部分是我们如何对模块进行编程,以便它更新所有网络表中的特定列值。每个表的架构完全相同。
编辑:我总是忘记添加一些东西:@。我知道这可以通过困难的方式完成,在代码后面运行一个循环。但有没有更简单、省事的方法呢?就像一些数据控制可以让这种情况下的生活变得更轻松一些?
更新:
我尝试使用后面的代码来做到这一点。我制作了一个网格视图并使用项目模板显示数据,并在第二个模板中提供了一个文本框。然后单击按钮,我正在运行此代码:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings[0].ToString());
foreach(GridViewRow gvr in GridView1.Rows)
{
TextBox tb = (TextBox)gvr.FindControl("New Value");
Label lbl = (Label)gvr.FindControl("Old Value");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (lbl.Text != tb.Text)
{
try //updation if primary key constraint is not broken
{
con.Open();
cmd.CommandText = myupdatecommand; /*this is not the actual command that I'm passing, the command I'm passing does contain the values lbl.Text & tb.Text. This is just to make it a better read.*/
cmd.ExecuteNonQuery();
}
catch (SqlException sqe)
{
if (sqe.ErrorCode == -2146232060)//if primary key constraint is broken
{
try
{
//delete the row from the table that contains only unique phone names
cmd.CommandText = deletecommand;
cmd.ExecuteNonQuery();
}
catch { }
}
}
finally
{
con.Close();
}
//Update table2
try //only updation as here the phone name is not a primary key
{
con.Open();
cmd.CommandText = updatetable2;
cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
con.Close();
}
.
.
.
//similarily update rest of the tables
.
.
.
Response.Redirect(Request.Url.ToString());
}
}
当我运行此代码时,一切都会顺利进行,但是当我们一次更新网格中的多行时,更新仅发生在第一个编辑的行,即其他编辑的行保持不变。
我知道这一定是我在这里错过的一件非常小的事情,但我无法进一步进行:(
非常感谢有关此事的任何帮助。提前致谢!
PS- 我使用 ASP.Net 3.5,以 C# 作为代码隐藏,以 SQL Server 2005 作为后端。
The Scenario:
I have a mobile phone comparing asp.net website which displays deals of various networks. The data about the deals we receive from the networks is in an excel sheet. We import the data from the excel sheets to the tables of each network in our database. Now, the data that we receive is not consistent for every network, as in, a network may name give the phone name as 'curve 8250' and another may give it as '8250 curve' and another might give it as '8250curve'.
Now, we have another module which allows the user to view the deals of a particular handset available on all the networks. To make this module work, what we need to do id make sure that the phone names are consistent for all the networks.
For this, I am planning to make a module for the webadmin which displays the phone names(probably in a gridview) from all the network tables, and the webmaster could edit the phone names so as to make them consistent. The retrieval of the distinct column names from all the tables was easy, and that is done.
The Problem:
Now, the real part is that how can we program the module so that it updates the particular column values in all the network tables. The schema of each table is exactly the same.
Edit: I always forget to add something :@ . I know it can be done the hard way, in code behind, running a loop. But is there any simpler, hassle free way out there? like some datacontrol that would make life a bit easier in this situation?
Update:
I tried doing this using code behind. I made a gridview and displayed the data using item templates, and also provided a textbox in a second template. Then on a button click, I'm running this code:
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings[0].ToString());
foreach(GridViewRow gvr in GridView1.Rows)
{
TextBox tb = (TextBox)gvr.FindControl("New Value");
Label lbl = (Label)gvr.FindControl("Old Value");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
if (lbl.Text != tb.Text)
{
try //updation if primary key constraint is not broken
{
con.Open();
cmd.CommandText = myupdatecommand; /*this is not the actual command that I'm passing, the command I'm passing does contain the values lbl.Text & tb.Text. This is just to make it a better read.*/
cmd.ExecuteNonQuery();
}
catch (SqlException sqe)
{
if (sqe.ErrorCode == -2146232060)//if primary key constraint is broken
{
try
{
//delete the row from the table that contains only unique phone names
cmd.CommandText = deletecommand;
cmd.ExecuteNonQuery();
}
catch { }
}
}
finally
{
con.Close();
}
//Update table2
try //only updation as here the phone name is not a primary key
{
con.Open();
cmd.CommandText = updatetable2;
cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
con.Close();
}
.
.
.
//similarily update rest of the tables
.
.
.
Response.Redirect(Request.Url.ToString());
}
}
When I run the this code, everything happens smoothly, but when we update more than one row in the grid at a time, the updation only occurs for the first edited row, the other edited rows are remaining the same.
I know it must be a very small thing that I'm missing out on here, but I'm not able to proceed further :(
Any help on the matter is highly appreciated. Thanks in advance!
PS- I'm using ASP.Net 3.5, with c# as code behind, and SQL Server 2005 as back-end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,我假设您正在使用 LINQ-to-Sql,但理论上,这并不重要,基本原理是相同的。
您将需要一组连接字符串,每个数据库对应一个连接字符串。想必你已经有了这个。
差不多就这样了。
您可以将“在此处更新”部分作为 lambda 函数传递。
OK, I'm going to assume you are using LINQ-to-Sql, but in theory, it shuoldn't matter, the basic principle is the same.
You will need a collection of connection string, one for each each database. Presumably you already have this.
That's pretty much it.
You could pass the "do update here" part in as a lambda function.
说实话,我现在都不好意思回答自己的问题。
问题只是我错误地将 Response.Redirect(Request.Url.ToString()); 放入
if 循环内,而循环本身位于 foreach 内循环。
我什么时候才能停止犯愚蠢的错误:|
To be honest, I'm now embarrassed to answer my own question.
The problem was just that I had mistakenly put the
Response.Redirect(Request.Url.ToString());
inside the
if
loop which itself is inside theforeach
loop.When will I stop doing silly mistakes :|