2014-12-08 86 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; 
     } 
    } 

} 
相關問題