【发布时间】:2014-07-02 01:39:18
【问题描述】:
我的代码如下。
SqlCommand command = new SqlCommand();
command.Parameters.AddWithValue("@AccountId", accountNumberLong);
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT * FROM T_POSTAGE_DISCOUNT");
sqlQuery = sql.ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = sqlQuery;
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dsDiscounts, "Discounts");
}
到目前为止,它工作正常。我想向这个数据集添加另一个表。然后我就这样了。
command.Parameters.AddWithValue("@DISCOUNT_LEVEL_NBR",discountLevelNBR);
sql.AppendLine("select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR");
sqlQuery = sql.ToString();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = sqlQuery;
SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
dataAdapter.Fill(dsDiscounts,"CONTRACT");
}
它再次添加所有表格。我想将 onlt 合同表添加到现有数据集。
我该怎么做?(c#)
【问题讨论】:
-
我认为您正在再次附加查询“sql.AppendLine("select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR");”,因此当您第二次执行时,它会同时执行这两个.. .
-
其实它是一个单独的查询。两个查询中的表名不同。我现在该怎么办?
-
创建一个新的字符串生成器 StringBuilder sql1 = new StringBuilder();然后 sql1.AppendLine("select * from T_CONTRACT WHERE DISCOUNT_LEVEL_NBR=@DISCOUNT_LEVEL_NBR"); sqlQuery = sql1.ToString();
-
看看这个link
标签: c# sql-server