CSV 파일에서 SQL 데이터베이스로 0.0000 숫자 (예 : 0.0125)를 구문 분석하려고합니다.SQL에 0.0000 숫자를 구문 분석하려고 시도했습니다.
문제는 내 데이터베이스에 부딪 힐 때입니다. 그것은 정상적인 숫자로 바뀌 었습니다!
CREATE TABLE RatesImport
(
"CallType" VarChar(30),
"ChargeCode" VarChar(30),
"Destination" VarChar(30),
"TariffUsed" VarChar(30),
"Peak" Real,
나는에 문제가 있어요 필드는 "피크"입니다 아래
내 코드의 조각입니다 :'--First create a datatable with the same cols as CSV file, the cols order in both should be same
Dim table As New DataTable()
table.Columns.Add("CallType", GetType(String))
table.Columns.Add("ChargeCode", GetType(String))
table.Columns.Add("Destination", GetType(String))
table.Columns.Add("TariffUsed", GetType(String))
table.Columns.Add("Peak", GetType(Double))
table.Columns.Add("OffPeak", GetType(Double))
table.Columns.Add("Weekend", GetType(Double))
table.Columns.Add("Setup", GetType(Double))
table.Columns.Add("MinimumCharge", GetType(Double))
table.Columns.Add("ChargeCap", GetType(Integer))
table.Columns.Add("InitialUnits", GetType(Integer))
table.Columns.Add("InitialCharge", GetType(Integer))
table.Columns.Add("InitialPeak", GetType(Integer))
table.Columns.Add("InitialOffPeak", GetType(Integer))
table.Columns.Add("InitialWeekend", GetType(Integer))
table.Columns.Add("BillingUnit", GetType(Integer))
table.Columns.Add("MinimumUnits", GetType(Integer))
table.Columns.Add("RateType", GetType(String))
'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,MinimumCharge,ChargeCap,InitialUnits,InitialCharge,InitialPeak,InitialOffPeak,InitialWeekend,BillingUnit,MinimumUnits,RateType) VALUES (@CallType,@ChargeCode,@Destination,@TariffUsed,@Peak,@OffPeak,@Weekend,@Setup,@MinimumCharge,@ChargeCap,@InitialUnits,@InitialCharge,@InitialPeak,@InitialOffPeak,@InitialWeekend,@BillingUnit,@MinimumUnits,@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")
라인으로 여기
내 dB 구조입니다 .Add ("@ Peak", SqlDbType.Int, 5, "Peak")는 코드 위에있는 그대로 작동하지만 숫자를 0 또는 1로 반올림합니다.하지만 .Add ("@ Peak", SqlDbType.Real, "Peak")로 변경하면 유형이 데이터베이스 열과 일치하므로 다음 오류가 발생합니다.
문자열 "Peak"에서 유형 'Integer'로의 변환이 유효하지 않습니다.
도움을 주시면 대단히 감사하겠습니다.
업데이트 :
- 피크
- 0.0822
- 0.0678
- :
나는 아래 해당 열의 데이터의 예입니다, 내 CSV 파일에 피크라는 열이 0.1232
- 0.2645
- 0.1428
- 0.2673
가져 오려는 파일의 처음 몇 줄을 게시하십시오. –
데이터베이스에 문제가 있습니다. 내가 볼 수 있듯이, Peek는 INT 데이터베이스입니다. 십진수를 저장하려면 변경해야합니다. – fnightangel
CSV 파일에 열 이름에 "Peak"가있는 머리글 행이 있습니까? –