Vb.net 正在尝试将0.0000数字解析为SQL

Vb.net 正在尝试将0.0000数字解析为SQL,vb.net,types,floating-point,decimal,Vb.net,Types,Floating Point,Decimal,我试图将CSV文件中的0.0000数字(即0.0125)解析到SQL数据库中 问题是当它进入我的数据库时。它已经变成了一个正常的数字 以下是我的数据库结构: CREATE TABLE RatesImport ( "CallType" VarChar(30), "ChargeCode" VarChar(30), "Destination" VarChar(30), "TariffUsed" VarChar(30), "Peak" Real, 我遇到麻烦的领域是“Peak” 下面是我的代码片段:

我试图将CSV文件中的0.0000数字(即0.0125)解析到SQL数据库中

问题是当它进入我的数据库时。它已经变成了一个正常的数字

以下是我的数据库结构:

CREATE TABLE RatesImport
(
"CallType" VarChar(30),
"ChargeCode" VarChar(30),
"Destination" VarChar(30),
"TariffUsed" VarChar(30),
"Peak" Real,
我遇到麻烦的领域是“Peak”

下面是我的代码片段:

    '--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")
”--首先创建一个与CSV文件具有相同COL的数据表,两个文件中的COL顺序应该相同
Dim表作为新数据表()
table.Columns.Add(“CallType”,GetType(String))
table.Columns.Add(“ChargeCode”,GetType(String))
table.Columns.Add(“目的地”,GetType(字符串))
table.Columns.Add(“Tariffuse”,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))
表.Columns.Add(“最小费用”,GetType(双精度))
table.Columns.Add(“ChargeCap”,GetType(整数))
table.Columns.Add(“InitialUnits”,GetType(Integer))
table.Columns.Add(“InitialCharge”,GetType(整数))
table.Columns.Add(“InitialPeak”,GetType(Integer))
table.Columns.Add(“InitialOffPeak”,GetType(整数))
table.Columns.Add(“InitialWeekend”,GetType(整数))
table.Columns.Add(“BillingUnit”,GetType(整数))
table.Columns.Add(“最小单位”,GetType(整数))
table.Columns.Add(“RateType”,GetType(String))
'打开文件对话框并存储文件名'
将openFileDialog1变暗为新OpenFileDialog
将strFileName设置为字符串
openFileDialog1.InitialDirectory=“C:\Users\Barry\Documents\Indigo Billing dB\Daisy通话费率”
openFileDialog1.Filter=“CSV文件(*.CSV)|*.CSV”
openFileDialog1.FilterIndex=2
openFileDialog1.RestoreDirectory=True
如果openFileDialog1.ShowDialog=Windows.Forms.DialogResult.OK,则
如果结束
strFileName=openFileDialog1.SafeFileName
如果strFileName为“”,则
'--TextField解析器用于读取文件
Dim解析器作为新的FileIO.TextFieldParser(openFileDialog1.FileName)
MessageBox.Show(“加载新费率卡…”,“Indigo账单”_
MessageBoxButtons.OK,MessageBoxIcon.Information)
parser.Delimiters=newstring(){,“}”字段用逗号分隔
parser.HasFieldsEnclosedInQuotes=True'每个值都用双引号括起来
parser.TrimWhiteSpace=True
“--跳过第一行,它是标题
parser.ReadLine()
“--将所有行添加到datatable
直到parser.EndOfData=True为止
table.Rows.Add(parser.ReadFields())
环
“--创建SQL查询
Dim strSql As String=“插入DaisyRatesImport(调用类型、费用代码、目的地、使用时间、峰值、非峰值、周末、设置、最低费用、费用上限、初始单位、初始费用、初始峰值、初始OffPeak、初始周末、账单单位、最小单位、费率类型)值(@CallType、@ChargeCode、@Destination、@Tariffuse、@Peak、@OffPeak、@Weekend、@Setup、@MinimumCharge、@ChargeCap、@InitialUnits、@InitialCharge、@InitialPeak、@InitialOffPeak、@InitialWeekend、@BillingUnits、@MinimumUnits、@RateType)”
Dim SqlconnectionString As String=“server=barry laptop\SQLEXPRESS;database=Test;integrated security=yes”
将连接用作新的SqlClient.SqlConnection(SqlconnectionString)
将cmd设置为新的SqlClient.SqlCommand(strSql,connection)'创建命令对象并添加参数
使用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(“@Peak”,SqlDbType.Int,5,“Peak”)起作用,但将数字舍入为0或1

但是如果我改为.Add(“@Peak”,SqlDbType.Real,“Peak”),那么该类型与我的数据库列匹配-我会得到以下错误

从字符串“Peak”到类型“Integer”的转换无效

任何帮助,非常感谢

更新:

我的CSV文件中有一个名为Peak的列,下面是该列中数据的示例:

  • 高峰
  • 0.0822
  • 0.0678
  • 0.1232
  • 0.2645
  • 0.1428
  • 0.2673

您正在尝试将字符串“Peak”转换为SqlDbType。Real,您需要从以前填充的数据表中读取值。现在,您正在使用字符串“CallType”、“ChargeCode”、“Peak”填充数据库,…但不是值。

请发布您试图导入的文件的前几行。问题在于您的数据库。正如我所见,Peek在数据库中是INT。您需要更改为存储十进制。CSV文件是否包含带有“Peak”的标题行在列名中?即使在循环之前先调用
ReadLine
,它似乎应该跳过第一行(包含字符串“Peak”),它一定不是。您需要跟踪代码以找出原因。我不确定我是否理解-我不是从数据库中提取这些值,它们来自CSV文件,我正在尝试导入到dB@user3580480他说
.Add(“@Peak”,SqlDbType.Int,5,“Peak”)
试图添加文字“Peak”作为字符串而不是从CSV读取的峰值值。CSV包含文本,因此您需要在添加到DBThreak@Proputix之前转换为正确的数据类型,这很有意义,因为CSV是一个文本文件:)如何转换值。你能提供一个代码示例吗?感谢
Convert.ToInteger
Convert.ToDouble
Convert.ToSingle
或为了安全起见并测试数据完整性,
Double.TryParse
Integer.TryParse