C# 使用adapter.update()从数据表更新SQL时出错

C# 使用adapter.update()从数据表更新SQL时出错,c#,datagridview,datatable,type-conversion,sqlcommand,C#,Datagridview,Datatable,Type Conversion,Sqlcommand,根据到目前为止的答案,我似乎没有正确地创建参数,也没有正确地传递值。我想从DataTable更新SQL表(DataTable是从SQL表填充的,列名相同),当我创建参数时,我认为第二个参数代表DataTable列。在将数据表“DWFieldScale”的值传递给SQL列DWFieldScale的地方,我如何设置它???(我创建的每个其他参数都相同) command.Parameters.AddWithValue(“@DWFieldScale”、“DWFieldScale”) 调用adapter.

根据到目前为止的答案,我似乎没有正确地创建参数,也没有正确地传递值。我想从DataTable更新SQL表(DataTable是从SQL表填充的,列名相同),当我创建参数时,我认为第二个参数代表DataTable列。在将数据表“DWFieldScale”的值传递给SQL列DWFieldScale的地方,我如何设置它???(我创建的每个其他参数都相同)

command.Parameters.AddWithValue(“@DWFieldScale”、“DWFieldScale”)

调用adapter.update()时出现SQL异常,因为我不知道如何在主题标题中正确设置适配器。我和C#一起工作才几个月,所以我还是个新手

无论如何,我已经尝试了十几种方法,并且已经达到了我“可能”在正确的轨道上的程度,但是我得到了一个“无法将NVARCHAR转换为INT”的问题,让我困扰的是,这是一个绑定到datatable的DataGridView,其中列的类型为INT。(在SQL中,列可以为null,并且有null)

  • NVARCHAR来自哪里
  • 如果NVARCHAR来自DataGridView,为什么它不使用匹配的类型自动生成
  • 我可以在哪里进行转换以允许我的da.Update(tblvAttributes);指挥工作?在定义参数时,我尝试过几种方法,但一定不能控制住我的下巴。帮助
  • 我的绑定代码:

    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, " +
                "DWPhysicalTableName=@DWPhysicalTableName, DWFieldName=@DWFieldName, DataDomain=@DataDomain," +
                "DWFieldDataType=@DWFieldDataType, DWFieldLength=@DWFieldLength, DWFieldScale=@DWFieldScale," +
                "SourceAttributeSID=@SourceAttributeSID  " +
    
                "WHERE DWPhysicalSchemaName = @DWPhysicalSchemaName and DWPhysicalTableName= @DWPhysicalTableName and DWFieldName=@DWFieldName", 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;
    

    在代码的两行中,我认为您正在为int参数指定String值,我的意思是:

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

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

    如果数据库字段为“int”,则下一行将给出错误:

    它将给出一个错误,因为您将字符串“DWFieldScale”作为值传递给字段。command.Parameters背后的思想是控制进行您所需的任何转换

    见此:

    NVARCHAR是连接认为您正试图传递给参数的类型。这是一种数据库字段类型

    另外,下面这句话很奇怪:

    if (!String.IsNullOrWhiteSpace("DWFieldScale"))
    
    String.IsNullOrWhiteSpace用于“变量”。您正在传递一个常量字符串。函数的结果将始终为true,因为存在字符串;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;
    
    command.Parameters.Add("@DWFieldScale", SqlDbType.Int);
    
    因此,您必须传递一个值,可以是一个常数,一个具有相同类型的变量,一个具有相同类型的函数的结果,等等。但实际上,必须是您希望在sql命令中出现的值

    但这是您想要执行命令的时候。 如果要将其绑定到datagrid或类似的类型,只需添加参数。不要传递值,因为这些值将在更新datagrid时设置

    因此,只需使用以下行:

    command.Parameters.Add("@DWFieldScale", SqlDbType.Int);
    command.Parameters["@DWFieldScale"].Value = 10;
    
    command.Parameters.Add("@DWFieldScale", SqlDbType.Int);
    
    让视图为您处理这些值

    这里有一个关于如何使用数据集(内存中)的好例子

    这个例子是针对“Select”语句的,但您会得到这样的想法:)

    下面是有关SQLDataAdapter的一些信息:

    我认为问题是因为在参数集合中添加了错误的值。在代码中,所有列都添加了字符串值。可能有些列是int类型的

    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");
    

    对于下一个刚开始的程序员,这是我在编辑绑定到DataTable的DataGridView时自动更新SQL数据库/表的完整解决方案:

    绑定:(将dgView、dTable和PositionChange事件绑定在一起)

    创建要从中执行适配器更新的事件: 私有void bindingSource1\u位置已更改(对象发送方,事件参数e) { var config=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); AppSettingsSection appSettingSection=(AppSettingsSection)config.GetSection(“cbSettings”); SqlConnection vx130=新的SqlConnection(appSettingSection.Settings[cbRegion.SelectedItem.ToString()]值); SqlDataAdapter da=CreateSQLAdapter(vx130); da.Update(tblvAttributes); }

    设置SQL适配器:(所有优点都在那里。它也很详细。另一种方法是重新表述语句以调用SQL存储过程。我没有。)


    此行:command.Parameters.AddWithValue(“@DWFieldScale”、“DWFieldScale”);如果DWFieldScale为int,则将给出错误,因为您将字符串“DWFieldScale”作为值传递给字段。命令.Parameters背后的思想是控制进行所需的任何转换。当您发现答案有用时,请向上投票。别忘了选择“解决”你的问题或疑问的答案。这有助于其他用户和愿意帮助的人:)听起来我的基本问题是我没有传递我认为我传递的东西或我想要的东西。我的目的是将列从datatable传递到SQL表。我该怎么做?我不太明白。。。你想更新你的数据库,对吗?那么,你已经成功了一半。主要问题是必须将值传递给参数。您只是传递“字符串”,实际上是字段的名称。我会在答案中举例说明如何做到这一点。哦,谢谢!我已经头痛两天了。我还不太擅长C#。你完全明白!没有问题:)您可能必须将SQLDataAdapter“绑定”到DataGridView(我在您展示的代码中没有看到这样做的代码)我确实读过那篇文章
    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, " +
                    "DWPhysicalTableName=@DWPhysicalTableName, DWFieldName=@DWFieldName, DataDomain=@DataDomain," +
                    "DWFieldDataType=@DWFieldDataType, DWFieldLength=@DWFieldLength, DWFieldScale=@DWFieldScale," +
                    "SourceAttributeSID=@SourceAttributeSID, ResolvedValue=@ResolvedValue, @PointedToField=@PointedToField," +
                    "MapComments=@MapComments, PrimaryKeyEntitySID=@PrimaryKeyEntitySID, SpecialHandlingFlag=@SpecialHandlingFlag," +
                    "DWFieldTechnicalDescription=@DWFieldTechnicalDescription, BuildStatus=@BuildStatus  " +
    
                    "WHERE DWPhysicalSchemaName = @DWPhysicalSchemaName and DWPhysicalTableName= @DWPhysicalTableName and DWFieldName=@DWFieldName", 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;
            }
        }
    
    }