2014-12-08 21 views
1

根据迄今为止的答案,看起来我没有正确创建参数,也没有正确传递值。我想从DataTable更新SQL表(数据表从SQL表填充,列名相同)当我创建参数时,我认为第二个参数表示了数据表列。如何将数据表“DWFieldScale”的值传递给SQL列DWFieldScale? (与我创建的每个其他参数相同)使用adapter.update更新数据表中的SQL时出错()

command.Parameters.AddWithValue(“@ DWFieldScale”,“DWFieldScale”);

当adapter.update()被调用时,我得到一个SQL异常,因为我不明白如何正确设置主题标题中的适配器。几个月前我一直在使用C#,所以我仍然很绿。

无论如何,我已经尝试了十几件事情,并已到了'我可以'在正确的轨道上,但我得到一个'不能转换NVARCHAR INT'什么让我困扰这是这是一个DataGridView绑定到列类型为INT的数据表。 (在SQL中,该列可为空且有空值)

  1. NVARCHAR从哪里来?
  2. 如果NVARCHAR来自DataGridView,为什么不自动生成匹配类型?
  3. 我在哪里可以进行转换以允许我的da.Update(tblvAttributes);指挥工作?我在定义参数时尝试了几件事,但决不能让我的下巴保持正确。 HELP

我的绑定代码:

private void tvVX130_AfterSelect(object sender, TreeViewEventArgs e) 
     { 
      string t = tvVX130.SelectedNode.Text; 
      BindingSource bs1 = new BindingSource(); 
      bs1.PositionChanged += bindingSource1_PositionChanged; 
      bs1.DataSource = tblvAttributes; 
      dgvVX130.DataSource = bs1; 
      string dwTN = tvVX130.SelectedNode.Text.Substring(0, tvVX130.SelectedNode.Text.IndexOf(" - ")); 
      bs1.Filter = "DWPhysicalTableName = '" + dwTN + "' AND DWPhysicalSchemaName = '" + t.Substring(t.IndexOf(" - ") + 5) + "'"; 
      dgvVX130.DataSource = bs1; 

     public static SqlDataAdapter CreateSQLAdapter(SqlConnection vx130) 
    { 
     SqlDataAdapter da = new SqlDataAdapter(); 

     command = new SqlCommand(
      "UPDATE [Meta].[AttributeMap] "+ 
      "SET DatabaseName = @DatabaseName, DWPhysicalSchemaName = @DWPhysicalSchemaName, " + 
      "[email protected], [email protected], [email protected]," + 
      "[email protected], [email protected], [email protected]," + 
      "[email protected] " + 

      "WHERE DWPhysicalSchemaName = @DWPhysicalSchemaName and DWPhysicalTableName= @DWPhysicalTableName and [email protected]", vx130); 

     command.Parameters.AddWithValue("@DatabaseName", "DatabaseName"); 
     command.Parameters.AddWithValue("@DWPhysicalSchemaName", "DWPhysicalSchemaName"); 
     command.Parameters.AddWithValue("@DWPhysicalTableName", "DWPhysicalTableName"); 
     command.Parameters.AddWithValue("@DWFieldName", "DWFieldName"); 
     command.Parameters.AddWithValue("@DWFieldDataType", "DWFieldDataType"); 
     command.Parameters.AddWithValue("@DWFieldLength", "DWFieldLength"); 
     //command.Parameters.AddWithValue("@DWFieldScale", "DWFieldScale"); gives can't convert NVARCHAR to INT 

     //if (!String.IsNullOrWhiteSpace("DWFieldScale"))  Doesn't recognize "DWFieldScale" as column 
     // command.Parameters.AddWithValue("@DWFieldScale", "DWFieldScale"); 
     //else 
     // command.Parameters.AddWithValue("@DWFieldScale", DBNull.Value); 

     //command.Parameters.Add("@DWFieldScale", SqlDbType.Int); 
     //command.Parameters["@DWFieldScale"].Value = "DWFieldScale"; Doesn't recognize "DWFieldScale" as column 

     //command.Parameters.AddWithValue("@DWFieldScale", int.Parse("DWFieldScale".ToString())); gives input incorrect format 

     command.Parameters.AddWithValue("@SourceAttributeSID", "SourceAttributeSID"); //this is also integer 

     da.UpdateCommand = command; 
+0

此行:command.Parameters.AddWithValue(“@ DWFieldScale”,“DWFieldScale”);如果DWFieldScale是一个int,则会发生错误,因为您将值作为值传递给字段“DWFieldScale”。命令背后的想法。参数控制是为了进行任何需要的转换。 – Jauch 2014-12-08 00:36:58

+0

当你找到有用的答案,upvote他们。不要忘记选择“解决”你的问题或怀疑的答案。这有助于其他用户和愿意帮助的人:) – Jauch 2014-12-08 00:53:37

回答

1

以下行:如果数据库字段是一个“INT”会给出错误的:

command.Parameters.AddWithValue("@DWFieldScale", "DWFieldScale"); 

它将因为您将字符串“DWFieldScale”作为值传递给字段,因此会发生错误。命令背后的想法。参数控制是为了进行任何需要的转换。

看到这个:

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue%28v=vs.110%29.aspx

的NVARCHAR是连接认为你试图传递到参数的类型。这是一个数据库字段类型。

另外,下面的行是奇怪:

if (!String.IsNullOrWhiteSpace("DWFieldScale")) 

字符串。IsNullOrWhiteSpace旨在用于“变量”。你传递一个常量字符串。函数的结果将始终为真,因为存在字符串,并且if的结果始终为FALSE,因为您正在否定函数的结果。

而在最后,这两条线将无法在一开始同样的原因,你设置的int参数,而是传递一个字符串值:

command.Parameters.Add("@DWFieldScale", SqlDbType.Int); 
command.Parameters["@DWFieldScale"].Value = "DWFieldScale"; 

使用的正确方法参数更像是这样的:

command.Parameters.Add("@DWFieldScale", SqlDbType.Int); 
command.Parameters["@DWFieldScale"].Value = 10; 

所以,你必须通过一个价值,才能BA恒定的,与同类型的变量,与同类型的函数的结果,等等。但实际上,要是你想要在sql命令中使用的valyue。

但是这是当你想执行命令。 如果你将它绑定到一个数据网格或类型的东西,只需添加参数。不要传递值,因为更新数据网格时将设置值。

因此,简单地用线这样的:

command.Parameters.Add("@DWFieldScale", SqlDbType.Int); 

而让视图照顾值的为您服务。

在这里有关于如何使用的数据集(内存)

http://msdn.microsoft.com/en-us/library/system.data.dataset%28v=vs.110%29.aspx

的例子一个很好的例子就是“选择”的声明,但你会得到的想法:)

这里关于SqlDataAdapter的一些信息: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter%28v=vs.110%29.aspx

+0

这听起来像我的基本问题是我没有通过我认为我经过或我想要的东西。 我的意图是将我的数据表中的列传递给我的SQL表。我怎么做? – 2014-12-08 00:56:54

+0

我不太明白...你想更新你的数据库,对不对?所以,你在中途。主要的问题是你必须将VALUES传递给参数。你只是传递“字符串”,这实际上是字段的名称。我将在如何做到这一点的答案中举一个例子。 – Jauch 2014-12-08 01:05:47

+0

噢谢谢你!我一直在打我的头2天。我还不是很擅长C#。 你完全理解! – 2014-12-08 01:07:52

0

在你的两行代码,我认为你是分配字符串INT参数我的意思是这些:

command.Parameters.AddWithValue("@DWFieldLength", "DWFieldLength"); 
command.Parameters.AddWithValue("@SourceAttributeSID", "SourceAttributeSID"); 

试将您的参数值更改为int您还需要更改您的命令文本并将你的varchar参数放入单个qoto标记中;然而,对于这种类型的数据库操作,最好使用存储过程而不是使用纯文本

0

我认为这个问题是因为你加错了价值参数collection.in你的代码,所有列,添加字符串值。也许一些列整型

command.Parameters.AddWithValue("@DatabaseName", "DatabaseName"); 
    command.Parameters.AddWithValue("@DWPhysicalSchemaName", "DWPhysicalSchemaName"); 
    command.Parameters.AddWithValue("@DWPhysicalTableName", "DWPhysicalTableName"); 
    command.Parameters.AddWithValue("@DWFieldName", "DWFieldName"); 
    command.Parameters.AddWithValue("@DWFieldDataType", "DWFieldDataType"); 
    command.Parameters.AddWithValue("@DWFieldLength", "DWFieldLength"); 
1

在接下来的程序员开始了,这是我的完整的解决方案编辑DataGridView的必然数据表时自动进行更新SQL数据库/表:

绑定:(联系在了一起dgView, dTable,& PositionChange事件)

private void tvVX130_AfterSelect(object sender, TreeViewEventArgs e) 
     { 
      string t = tvVX130.SelectedNode.Text; 
      BindingSource bs1 = new BindingSource(); 
      bs1.PositionChanged += bindingSource1_PositionChanged; 
      bs1.DataSource = tblvAttributes; 
      dgvVX130.DataSource = bs1; 
      string dwTN = tvVX130.SelectedNode.Text.Substring(0, tvVX130.SelectedNode.Text.IndexOf(" - ")); 
      bs1.Filter = "DWPhysicalTableName = '" + dwTN + "' AND DWPhysicalSchemaName = '" + t.Substring(t.IndexOf(" - ") + 5) + "'"; 
      dgvVX130.DataSource = bs1; 
     } 

创建从其中执行适配器更新事件: 私人无效bindingSource1_PositionChanged(对象发件人,EventArgs的) { var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); AppSettingsSection appSettingSection =(AppSettingsSection)config.GetSection(“cbSettings”); SqlConnection vx130 = new SqlConnection(appSettingSection.Settings [cbRegion.SelectedItem.ToString()] Value); SqlDataAdapter da = CreateSQLAdapter(vx130); da.Update(tblvAttributes); }

设置SQL适配器:(所有的好处都是,也是冗长的,另一种方法是重新调整语句来调用SQL存储过程,但我没有。)

public static SqlDataAdapter CreateSQLAdapter(SqlConnection vx130) 
     { 
      SqlDataAdapter da = new SqlDataAdapter(); 

      // Create the SelectCommand. 
      SqlCommand command = new SqlCommand("Select DatabaseName, DWPhysicalSchemaName, DWPhysicalTableName, " + 
       "DWFieldName ,DataDomain, DWFieldDataType, DWFieldLength, DWFieldScale, SourceAttributeSID, "+ 
       "ResolvedValue, PointedToField, MapComments, PrimaryKeyEntitySID, SpecialHandlingFlag, "+ 
       "DWFieldTechnicalDescription, BuildStatus from meta.attributemap", vx130); 

      da.SelectCommand = command; 

      // Create the InsertCommand. 
      command = new SqlCommand(
      "Insert Into [Meta].[AttributeMap] " + 
       "(DatabaseName, DWPhysicalSchemaName, DWPhysicalTableName, " + 
       "DWFieldName ,DataDomain, DWFieldDataType, DWFieldLength, DWFieldScale, SourceAttributeSID, " + 
       "ResolvedValue, PointedToField, MapComments, PrimaryKeyEntitySID, SpecialHandlingFlag, " + 
       "DWFieldTechnicalDescription, BuildStatus) " + 


      "Values (@DatabaseName, @DWPhysicalSchemaName, @DWPhysicalTableName, " + 
       "@DWFieldName ,@DataDomain, @DWFieldDataType, @DWFieldLength, @DWFieldScale, @SourceAttributeSID, " + 
       "@ResolvedValue, @PointedToField, @MapComments, @PrimaryKeyEntitySID, @SpecialHandlingFlag, " + 
       "@DWFieldTechnicalDescription, @BuildStatus)" , vx130); 

      // Add the parameters for the InsertCommand. 
      command.Parameters.Add(new SqlParameter("@DatabaseName", SqlDbType.VarChar)); 
      command.Parameters["@DatabaseName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DatabaseName"].SourceColumn = "DatabaseName"; 

      command.Parameters.Add(new SqlParameter("@DWPhysicalSchemaName", SqlDbType.VarChar)); 
      command.Parameters["@DWPhysicalSchemaName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWPhysicalSchemaName"].SourceColumn = "DWPhysicalSchemaName"; 

      command.Parameters.Add(new SqlParameter("@DWPhysicalTableName", SqlDbType.VarChar)); 
      command.Parameters["@DWPhysicalTableName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWPhysicalTableName"].SourceColumn = "DWPhysicalTableName"; 

      command.Parameters.Add(new SqlParameter("@DWFieldName", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldName"].SourceColumn = "DWFieldName"; 

      command.Parameters.Add(new SqlParameter("@DataDomain", SqlDbType.VarChar)); 
      command.Parameters["@DataDomain"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DataDomain"].SourceColumn = "DataDomain"; 

      command.Parameters.Add(new SqlParameter("@DWFieldDataType", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldDataType"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldDataType"].SourceColumn = "DWFieldDataType"; 

      command.Parameters.Add(new SqlParameter("@DWFieldLength", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldLength"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldLength"].SourceColumn = "DWFieldLength"; 

      command.Parameters.Add(new SqlParameter("@DWFieldScale", SqlDbType.Int)); 
      command.Parameters["@DWFieldScale"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldScale"].SourceColumn = "DWFieldScale"; 

      command.Parameters.Add(new SqlParameter("@SourceAttributeSID", SqlDbType.Int)); 
      command.Parameters["@SourceAttributeSID"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@SourceAttributeSID"].SourceColumn = "SourceAttributeSID"; 

      command.Parameters.Add(new SqlParameter("@ResolvedValue", SqlDbType.VarChar)); 
      command.Parameters["@ResolvedValue"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@ResolvedValue"].SourceColumn = "ResolvedValue"; 

      command.Parameters.Add(new SqlParameter("@PointedToField", SqlDbType.VarChar)); 
      command.Parameters["@PointedToField"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@PointedToField"].SourceColumn = "PointedToField"; 

      command.Parameters.Add(new SqlParameter("@MapComments", SqlDbType.VarChar)); 
      command.Parameters["@MapComments"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@MapComments"].SourceColumn = "MapComments"; 

      command.Parameters.Add(new SqlParameter("@PrimaryKeyEntitySID", SqlDbType.Int)); 
      command.Parameters["@PrimaryKeyEntitySID"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@PrimaryKeyEntitySID"].SourceColumn = "PrimaryKeyEntitySID"; 

      command.Parameters.Add(new SqlParameter("@SpecialHandlingFlag", SqlDbType.VarChar)); 
      command.Parameters["@SpecialHandlingFlag"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@SpecialHandlingFlag"].SourceColumn = "SpecialHandlingFlag"; 

      command.Parameters.Add(new SqlParameter("@DWFieldTechnicalDescription", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldTechnicalDescription"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldTechnicalDescription"].SourceColumn = "DWFieldTechnicalDescription"; 

      command.Parameters.Add(new SqlParameter("@BuildStatus", SqlDbType.VarChar)); 
      command.Parameters["@BuildStatus"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@BuildStatus"].SourceColumn = "BuildStatus"; 

      da.InsertCommand = command; 

      // Create the UpdateCommand. 
      command = new SqlCommand(
       "UPDATE [Meta].[AttributeMap] "+ 
       "SET DatabaseName = @DatabaseName, DWPhysicalSchemaName = @DWPhysicalSchemaName, " + 
       "[email protected], [email protected], [email protected]," + 
       "[email protected], [email protected], [email protected]," + 
       "[email protected], [email protected], @[email protected]," + 
       "[email protected], [email protected], [email protected]," + 
       "[email protected], [email protected] " + 

       "WHERE DWPhysicalSchemaName = @DWPhysicalSchemaName and DWPhysicalTableName= @DWPhysicalTableName and [email protected]", vx130); 

      command.Parameters.Add(new SqlParameter("@DatabaseName", SqlDbType.VarChar)); 
      command.Parameters["@DatabaseName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DatabaseName"].SourceColumn = "DatabaseName"; 

      command.Parameters.Add(new SqlParameter("@DWPhysicalSchemaName", SqlDbType.VarChar)); 
      command.Parameters["@DWPhysicalSchemaName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWPhysicalSchemaName"].SourceColumn = "DWPhysicalSchemaName"; 

      command.Parameters.Add(new SqlParameter("@DWPhysicalTableName", SqlDbType.VarChar)); 
      command.Parameters["@DWPhysicalTableName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWPhysicalTableName"].SourceColumn = "DWPhysicalTableName"; 

      command.Parameters.Add(new SqlParameter("@DWFieldName", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldName"].SourceColumn = "DWFieldName"; 

      command.Parameters.Add(new SqlParameter("@DataDomain", SqlDbType.VarChar)); 
      command.Parameters["@DataDomain"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DataDomain"].SourceColumn = "DataDomain"; 

      command.Parameters.Add(new SqlParameter("@DWFieldDataType", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldDataType"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldDataType"].SourceColumn = "DWFieldDataType"; 

      command.Parameters.Add(new SqlParameter("@DWFieldLength", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldLength"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldLength"].SourceColumn = "DWFieldLength"; 

      command.Parameters.Add(new SqlParameter("@DWFieldScale", SqlDbType.Int)); 
      command.Parameters["@DWFieldScale"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldScale"].SourceColumn = "DWFieldScale"; 

      command.Parameters.Add(new SqlParameter("@SourceAttributeSID", SqlDbType.Int)); 
      command.Parameters["@SourceAttributeSID"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@SourceAttributeSID"].SourceColumn = "SourceAttributeSID"; 

      command.Parameters.Add(new SqlParameter("@ResolvedValue", SqlDbType.VarChar)); 
      command.Parameters["@ResolvedValue"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@ResolvedValue"].SourceColumn = "ResolvedValue"; 

      command.Parameters.Add(new SqlParameter("@PointedToField", SqlDbType.VarChar)); 
      command.Parameters["@PointedToField"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@PointedToField"].SourceColumn = "PointedToField"; 

      command.Parameters.Add(new SqlParameter("@MapComments", SqlDbType.VarChar)); 
      command.Parameters["@MapComments"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@MapComments"].SourceColumn = "MapComments"; 

      command.Parameters.Add(new SqlParameter("@PrimaryKeyEntitySID", SqlDbType.Int)); 
      command.Parameters["@PrimaryKeyEntitySID"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@PrimaryKeyEntitySID"].SourceColumn = "PrimaryKeyEntitySID"; 

      command.Parameters.Add(new SqlParameter("@SpecialHandlingFlag", SqlDbType.VarChar)); 
      command.Parameters["@SpecialHandlingFlag"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@SpecialHandlingFlag"].SourceColumn = "SpecialHandlingFlag"; 

      command.Parameters.Add(new SqlParameter("@DWFieldTechnicalDescription", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldTechnicalDescription"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldTechnicalDescription"].SourceColumn = "DWFieldTechnicalDescription"; 

      command.Parameters.Add(new SqlParameter("@BuildStatus", SqlDbType.VarChar)); 
      command.Parameters["@BuildStatus"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@BuildStatus"].SourceColumn = "BuildStatus"; 

      da.UpdateCommand = command; 

      // Create the DeleteCommand. 
      command = new SqlCommand(
       "delete from vx130.Meta.AttributeMap " + 
        " where DWPhysicalSchemaName = @DWPhysicalSchemaName AND " + 
         " DWPhysicalTableName = @DWPhysicalTableName AND DWFieldName = @DWFieldName", vx130); 

      // Add the parameters for the DeleteCommand. 
      command.Parameters.Add(new SqlParameter("@DWPhysicalSchemaName", SqlDbType.VarChar)); 
      command.Parameters["@DWPhysicalSchemaName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWPhysicalSchemaName"].SourceColumn = "DWPhysicalSchemaName"; 

      command.Parameters.Add(new SqlParameter("@DWPhysicalTableName", SqlDbType.VarChar)); 
      command.Parameters["@DWPhysicalTableName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWPhysicalTableName"].SourceColumn = "DWPhysicalTableName"; 

      command.Parameters.Add(new SqlParameter("@DWFieldName", SqlDbType.VarChar)); 
      command.Parameters["@DWFieldName"].SourceVersion = DataRowVersion.Current; 
      command.Parameters["@DWFieldName"].SourceColumn = "DWFieldName"; 

      da.DeleteCommand = command; 

      return da; 
     } 
    } 

} 
相关问题