![](/img/trans.png)
[英]Why are my decimal values being rounded to integers in SQL insertions?
[英]Values being passed as strings and not integers
我試圖收集一系列值作為字符串,然后將它們插入到SQL Server dB中。 我只能使用'VarChar'正常工作,但是當我嘗試加載包含數字的列時會失敗。
它不斷拋出以下有關從字符串轉換為整數的錯誤。
“無法將參數值從字符串轉換為Int32。”
請有人幫我正確的語法。
以下是我的代碼的副本:
Dim table As New DataTable()
table.Columns.Add("CallType")
table.Columns.Add("ChargeCode")
table.Columns.Add("Destination")
table.Columns.Add("TariffUsed")
table.Columns.Add("Peak")
table.Columns.Add("OffPeak")
table.Columns.Add("Weekend")
table.Columns.Add("Setup")
table.Columns.Add("Minimum Charge")
table.Columns.Add("Charge Cap")
table.Columns.Add("Initial Units")
table.Columns.Add("Initial Charge")
table.Columns.Add("Initial Peak")
table.Columns.Add("Initial Off Peak")
table.Columns.Add("Initial Weekend")
table.Columns.Add("Billing Unit")
table.Columns.Add("Minimum Units")
table.Columns.Add(" RateType")
'open file dialog and store filename
Dim openFileDialog1 As New OpenFileDialog
Dim strFileName As String
openFileDialog1.InitialDirectory = "C:\Users\Barry\Documents\Indigo Billing dB\Daisy Call Rates"
openFileDialog1.Filter = "CSV files (*.csv)|*.csv"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
End If
strFileName = openFileDialog1.SafeFileName
If strFileName <> "" Then
'TextField Parser is used to read the files
Dim parser As New FileIO.TextFieldParser(openFileDialog1.FileName)
MessageBox.Show("New Rate Card Loaded...", "Indigo Billing", _
MessageBoxButtons.OK, MessageBoxIcon.Information)
parser.Delimiters = New String() {","} ' fields are separated by comma
parser.HasFieldsEnclosedInQuotes = True ' each of the values is enclosed with double quotes
parser.TrimWhiteSpace = True
'--First line is skipped , its the header
parser.ReadLine()
'-- Add all the rows to datatable
Do Until parser.EndOfData = True
table.Rows.Add(parser.ReadFields())
Loop
'--Create SQL query
Dim strSql As String = "INSERT INTO DaisyRatesImport (CallType,ChargeCode,Destination,TariffUsed,Peak,OffPeak,Weekend,Setup,Minimum Charge,Charge Cap,Initial Units,Initial Charge,Initial Peak,Initial Off Peak,Initial Weekend,Billing Unit,Minimum Units, RateType) VALUES (@CallType,@ChargeCode,@Destination,@TariffUsed,@Peak,@OffPeak,@Weekend,@Setup,@Minimum Charge,@Charge Cap,@Initial Units,@Initial Charge,@Initial Peak,@Initial Off Peak,@Initial Weekend,@Billing Unit,@Minimum Units, @RateType)"
Dim SqlconnectionString As String = "server=barry-laptop\SQLEXPRESS; database=Test; integrated security=yes"
Using connection As New SqlClient.SqlConnection(SqlconnectionString)
Dim cmd As New SqlClient.SqlCommand(strSql, connection) ' create command objects and add parameters
With cmd.Parameters
.Add("@CallType", SqlDbType.VarChar, 30, "CallType")
.Add("@ChargeCode", SqlDbType.VarChar, 30, "ChargeCode")
.Add("@Destination", SqlDbType.VarChar, 30, "Destination")
.Add("@TariffUsed", SqlDbType.VarChar, 30, "TariffUsed")
.Add("@Peak", SqlDbType.Int, 5, "Peak")
.Add("@OffPeak", SqlDbType.Int, 5, "OffPeak")
.Add("@Weekend", SqlDbType.Int, 5, "Weekend")
.Add("@Setup", SqlDbType.Int, 5, "Setup")
.Add("@Minimum Charge", SqlDbType.Int, 5, "Minimum Charge")
.Add("@Charge Cap", SqlDbType.Int, 5, "Charge Cap")
.Add("@Initial Units", SqlDbType.Int, 5, "Initial Units")
.Add("@Initial Charge", SqlDbType.Int, 5, "Initial Charge")
.Add("@Initial Peak", SqlDbType.Int, 5, "Initial Peak")
.Add("@Initial Off Peak", SqlDbType.Int, 5, "Initial Off Peak")
.Add("@Initial Weekend", SqlDbType.Int, 5, "Initial Weekend")
.Add("@Billing Unit", SqlDbType.Int, 5, "Billing Unit")
.Add("@Minimum Units", SqlDbType.Int, 5, "Minimum Units")
.Add("@ RateType", SqlDbType.VarChar, 30, " RateType")
End With
Dim adapter As New SqlClient.SqlDataAdapter()
adapter.InsertCommand = cmd
'--Update the original SQL table from the datatable
Dim iRowsInserted As Int32 = _
adapter.Update(table)
End Using
Else
MessageBox.Show("No File Selected", "Indigo Billing", _
MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
'displayes file name in Textbox1
TextBox1.Text = strFileName
End Sub
我在這里進行了搜索,並認為可能需要對其進行轉換,但是我不確定我要與之轉換的內容。
任何幫助,不勝感激。
謝謝
嘗試用以下數據類型標記列
table.Columns.Add("CallType", GetType(Integer))
table.Columns.Add("ChargeCode", GetType(Integer))
table.Columns.Add("Destination", GetType(string))
table.Columns.Add("TariffUsed", GetType(string))
我剛剛對它們進行了隨機標記。 這將對您有用,因為Column將具有特定的數據類型並解決您的轉換問題。
SQL命令的參數值包含兩個單詞: @Minimum Charge,@Charge Cap,@Initial Units
您不能這樣做。 改為執行此操作: @MinimumCharge,@ChargeCap,@InitialUnits
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.