2015-07-10 65 views
0

嗨我想通過SqlDatasource.InsertParameter插入一個浮點值到數據庫中,但執行SqlDatasource.Insert()函數後,我只看到它插入數據庫中的整數部分。我還調試並檢查了執行SqlDatasource.Insert()之前存儲在SqlDatasource中的值是否爲float。下面是我的代碼:通過SqlDatasource.InsertParameter發送浮動參數

從數據層功能:

 public static SqlDataSource DataSourceInsert(string sp, string[,] param) 
{ 
    float f; 
    ds = new SqlDataSource(); 
    if (cns.State != ConnectionState.Open) 
     cns.Open(); 
    ds.ConnectionString = ConfigurationManager.ConnectionStrings["cns"].ConnectionString; 
    ds.InsertCommandType = SqlDataSourceCommandType.StoredProcedure; 
    ds.InsertCommand = sp; 

    for (int i = 0; i < param.GetLength(1); i++) 
    { 
     ds.InsertParameters.Add(param[0, i], param[1, i]); 
     ds.InsertParameters[param[0, i]].DefaultValue = param[1, i]; 
     if(float.TryParse(param[1, i],out f)) 
      ds.InsertParameters[param[0, i]].Type = System.TypeCode.Double; 
    } 
    ds.Insert(); 
    return ds; 
} 

P參數

protected void DV_ItemInsert(object sender, DetailsViewInsertEventArgs e) 
{ 
    string procname = "spAddandInsertProduct"; 
    string[,] p = new string[2, 5]; 
    p[0, 0] = "ProductName"; 
    p[1, 0] = ((TextBox)((DetailsView)sender).Rows[0].Cells[1].Controls[0]).Text; 
    p[0, 1] = "Description"; 
    p[1, 1] = ((TextBox)((DetailsView)sender).Rows[1].Cells[1].Controls[0]).Text; 
    p[0, 2] = "IsPublished"; 
    p[1, 2] = ((TextBox)((DetailsView)sender).Rows[2].Cells[1].Controls[0]).Text; 
    p[0, 3] = "Quantity"; 
    p[1, 3] = ((TextBox)((DetailsView)sender).Rows[3].Cells[1].Controls[0]).Text; 
    p[0, 4] = "Price"; 
    p[1, 4] = ((TextBox)((DetailsView)sender).Rows[4].Cells[1].Controls[0]).Text; 
    ds = SQLInteractor.DataSourceInsert(procname, p); 

    ProdAdmin_insertDV.DataSource = ds; 
    ProdAdmin_insertDV.DataBind(); 
    GV_databind(); 
} 
+0

'param'的定義是什麼? – DavidG

+0

那不可讀。它是一個字符串數組。 –

+1

這並不能告訴我「參數」是什麼。另外,請在您的問題中編輯細節。 – DavidG

回答

0

有你需要檢查兩件事情: 一)什麼文化背景與Google合作,並做這些浮點值的字符串表示匹配?這可能是你的錯誤。 b)不太可能的情況是,您需要使用double操作,以便更好地處理轉換爲在數據庫端更高的轉換,或者因爲您的數據值需要轉換。