vb.net 2013错误 - “无法将参数从字符串转换为日期时间” [英] vb.net 2013 error - "Failed to convert parameter from string to a datetime"

查看:80
本文介绍了vb.net 2013错误 - “无法将参数从字符串转换为日期时间”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨老年人,

我的代码遇到了问题...每次运行代码我都会收到错误"无法将参数从字符串转换为日期时间"。"
以下是我的代码......我迫切需要你的帮助。非常感谢。 
$


Dim dgvconnection As New SqlConnection(" Data Source =(LocalDB)\ v11.0; AttachDbFilename = D:\ Visual Basic Project \Church Database System \ WindowsApplication1 \ cms.mdf; Integrated Security = True")

        Dim dgvcommand As SqlCommand = dgvconnection.CreateCommand()




       试试¥b $ b            '开放式连接

            dgvconnection.Open()



            '使用命名参数创建插入语句

            dgvcommand.CommandText ="插入成员(id,datemembregis,year,Title,FullName,MaidName,DOB,Age,Gender,HomeTown,POB,MemStatus,MemType,MaritalStatus,MarriageType,SpouseName,SpouseDeno,DOBap,PlaceofBap,DOConf, PlaceofConf,gengroup,sergroup,othergroup,daygroup,memsince,transfer)
values(@ id,@ datemembregis,@ year,@ title,@ fullname,@ maidname,@ dob,@ Age,@ gender,@ hometown, @ POB,@ MemStatus,@ mem类型,@ maritalstatus,@ marriagetype,@ SpouseName,@ SpouseDeno,@ dobap,@ placeofbap,@ doconf,@ placeofconf,@ gengroup,@ sergroup,@ othergroup,@ daygroup,@ memsince,@转印)"
$


            '将参数添加到命令参数集合中$
            dgvcommand.Parameters.Add(" @ id",SqlDbType.Int,50)

            dgvcommand.Parameters.Add(" @ datemembregis",SqlDbType.SmallDateTime)

            dgvcommand.Parameters.Add(" @ year",SqlDbType.NChar,10)

            dgvcommand.Parameters.Add(" @ title",SqlDbType.NChar,10)

            dgvcommand.Parameters.Add(" @ fullname",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ maidname",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ dob",SqlDbType.SmallDateTime)

            dgvcommand.Parameters.Add(" @ age",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ gender",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ hometown",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ pob",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ memstatus",SqlDbType.VarChar,50)



            dgvcommand.Parameters.Add(" @ memtype",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ maritalstatus",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ marriagetype",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ spousename",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ spousedeno",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ dobap",SqlDbType.SmallDateTime)

            dgvcommand.Parameters.Add(" @ placeofbap",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ doconf",SqlDbType.SmallDateTime)

            dgvcommand.Parameters.Add(" @ placeofconf",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ gengroup",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ sergroup",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ othergroup",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ daygroup",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ memsince",SqlDbType.VarChar,50)

            dgvcommand.Parameters.Add(" @ transfer",SqlDbType.VarChar,50)



            '准备重复执行的命令

            dgvcommand.Prepare()



            '要插入的数据

            For Each Data As DataGridViewRow in dgvExcelRegistration.Rows

               如果不是row.IsNewRow则为


                    dgvcommand.Parameters(" @ id")。Value = row.Cells(" id")。值

                    dgvcommand.Parameters(" @ datemembregis")。SqlValue = row.Cells(" DateReg")。值

                    dgvcommand.Parameters(" @ year")。Value = row.Cells(" year")。Value

                    dgvcommand.Parameters(" @ title")。Value = row.Cells(" Title")。值

                    dgvcommand.Parameters(" @ fullname")。Value = row.Cells(" FullName")。值

                    dgvcommand.Parameters(" @ maidname")。Value = row.Cells(" Maiden")。Value

                    dgvcommand.Parameters(" @ dob")。SqlValue = row.Cells(" DOB")。值

                    dgvcommand.Parameters(" @ age")。Value = row.Cells(" Age")。值

                    dgvcommand.Parameters(" @ gender")。Value = row.Cells(" Gender")。值

                    dgvcommand.Parameters(" @ hometown")。Value = row.Cells(" HomeTown")。值

                    dgvcommand.Parameters(" @ pob")。Value = row.Cells(" POB")。Value

                    dgvcommand.Parameters(" @ memstatus")。Value = row.Cells(" MemStatus")。值



                    dgvcommand.Parameters(" @ memtype")。Value = row.Cells(" MemType")。Value

                    dgvcommand.Parameters("@ maritalstatus")。Value = row.Cells(" MaritalStatus)。值

                    dgvcommand.Parameters(" @ marriagetype")。Value = row.Cells(" MaritalType")。值

                    dgvcommand.Parameters(" @ spousename")。Value = row.Cells(" SpouseName")。Value

                    dgvcommand.Parameters(" @ spousedeno")。Value = row.Cells(" SpouseDeno")。值

                    dgvcommand.Parameters(" @ dobap")。SqlValue = row.Cells(" DOBapt")。值

                    dgvcommand.Parameters(" @ placeofbap")。Value = row.Cells(" PlaceBaptism")。值

                    dgvcommand.Parameters(" @ doconf")。SqlValue = row.Cells(" ConfDate")。值

                    dgvcommand.Parameters("@ placeofconf")。Value = row.Cells("ConfPlace")。值

                    dgvcommand.Parameters(" @ gengroup")。Value = row.Cells(" GenGroup")。Value

                    dgvcommand.Parameters(" @ sergroup")。Value = row.Cells(" ServiceGrp")。值

                    dgvcommand.Parameters(" @ othergroup")。Value = row.Cells(" OtherGrp")。值

                    dgvcommand.Parameters(" @ daygroup")。Value = row.Cells(" DayBorn")。值

                    dgvcommand.Parameters(" @ memsince")。Value = row.Cells(" MemSince")。Value

                    dgvcommand.Parameters(" @ transfer")。Value = row.Cells(" TransferFrom")。值



                    dgvcommand.ExecuteNonQuery()

                    dgvcommand.Parameters.Clear()

               结束如果

           下一页



        C ex ex As Exception

            MessageBox.Show(ex.Message)



       最后是
            dgvconnection.Close()

       结束尝试

I am facing a problem with my code...each time i run the code i get the error "failed to convert parameter from string to a datetime".
Below is my code...I urgently need your help. Thanks a lot. 

Dim dgvconnection As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\Visual Basic Project\Church Database System\WindowsApplication1\cms.mdf;Integrated Security=True")
        Dim dgvcommand As SqlCommand = dgvconnection.CreateCommand()


        Try
            'open connection
            dgvconnection.Open()

            'create Insert statement with named parameters
            dgvcommand.CommandText = "Insert into members(id,datemembregis,year,Title,FullName,MaidName,DOB,Age,Gender,HomeTown,POB,MemStatus,MemType,MaritalStatus,MarriageType,SpouseName,SpouseDeno,DOBap,PlaceofBap,DOConf,PlaceofConf,gengroup,sergroup,othergroup,daygroup,memsince,transfer) values(@id,@datemembregis,@year,@title,@fullname,@maidname,@dob,@Age,@gender,@hometown,@pob,@MemStatus,@MemType,@maritalstatus,@marriagetype,@SpouseName,@SpouseDeno,@dobap,@placeofbap,@doconf,@placeofconf,@gengroup,@sergroup,@othergroup,@daygroup,@memsince,@transfer)"

            'add parameters to command parameters collection
            dgvcommand.Parameters.Add("@id", SqlDbType.Int, 50)
            dgvcommand.Parameters.Add("@datemembregis", SqlDbType.SmallDateTime)
            dgvcommand.Parameters.Add("@year", SqlDbType.NChar, 10)
            dgvcommand.Parameters.Add("@title", SqlDbType.NChar, 10)
            dgvcommand.Parameters.Add("@fullname", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@maidname", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@dob", SqlDbType.SmallDateTime)
            dgvcommand.Parameters.Add("@age", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@gender", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@hometown", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@pob", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@memstatus", SqlDbType.VarChar, 50)

            dgvcommand.Parameters.Add("@memtype", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@maritalstatus", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@marriagetype", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@spousename", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@spousedeno", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@dobap", SqlDbType.SmallDateTime)
            dgvcommand.Parameters.Add("@placeofbap", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@doconf", SqlDbType.SmallDateTime)
            dgvcommand.Parameters.Add("@placeofconf", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@gengroup", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@sergroup", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@othergroup", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@daygroup", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@memsince", SqlDbType.VarChar, 50)
            dgvcommand.Parameters.Add("@transfer", SqlDbType.VarChar, 50)

            'prepare command for repeated execution
            dgvcommand.Prepare()

            'data to be inserted
            For Each row As DataGridViewRow In dgvExcelRegistration.Rows
                If Not row.IsNewRow Then

                    dgvcommand.Parameters("@id").Value = row.Cells("id").Value
                    dgvcommand.Parameters("@datemembregis").SqlValue = row.Cells("DateReg").Value
                    dgvcommand.Parameters("@year").Value = row.Cells("year").Value
                    dgvcommand.Parameters("@title").Value = row.Cells("Title").Value
                    dgvcommand.Parameters("@fullname").Value = row.Cells("FullName").Value
                    dgvcommand.Parameters("@maidname").Value = row.Cells("Maiden").Value
                    dgvcommand.Parameters("@dob").SqlValue = row.Cells("DOB").Value
                    dgvcommand.Parameters("@age").Value = row.Cells("Age").Value
                    dgvcommand.Parameters("@gender").Value = row.Cells("Gender").Value
                    dgvcommand.Parameters("@hometown").Value = row.Cells("HomeTown").Value
                    dgvcommand.Parameters("@pob").Value = row.Cells("POB").Value
                    dgvcommand.Parameters("@memstatus").Value = row.Cells("MemStatus").Value

                    dgvcommand.Parameters("@memtype").Value = row.Cells("MemType").Value
                    dgvcommand.Parameters("@maritalstatus").Value = row.Cells("MaritalStatus").Value
                    dgvcommand.Parameters("@marriagetype").Value = row.Cells("MaritalType").Value
                    dgvcommand.Parameters("@spousename").Value = row.Cells("SpouseName").Value
                    dgvcommand.Parameters("@spousedeno").Value = row.Cells("SpouseDeno").Value
                    dgvcommand.Parameters("@dobap").SqlValue = row.Cells("DOBapt").Value
                    dgvcommand.Parameters("@placeofbap").Value = row.Cells("PlaceBaptism").Value
                    dgvcommand.Parameters("@doconf").SqlValue = row.Cells("ConfDate").Value
                    dgvcommand.Parameters("@placeofconf").Value = row.Cells("ConfPlace").Value
                    dgvcommand.Parameters("@gengroup").Value = row.Cells("GenGroup").Value
                    dgvcommand.Parameters("@sergroup").Value = row.Cells("ServiceGrp").Value
                    dgvcommand.Parameters("@othergroup").Value = row.Cells("OtherGrp").Value
                    dgvcommand.Parameters("@daygroup").Value = row.Cells("DayBorn").Value
                    dgvcommand.Parameters("@memsince").Value = row.Cells("MemSince").Value
                    dgvcommand.Parameters("@transfer").Value = row.Cells("TransferFrom").Value

                    dgvcommand.ExecuteNonQuery()
                    dgvcommand.Parameters.Clear()
                End If
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message)

        Finally
            dgvconnection.Close()
        End Try

推荐答案

异常的含义是无法将值转换为正确的类型。因此,如果传入一个无法转换为Date的字符串,则会抛出异常。

What the exception is saying is that a value can't be converted to the proper type. So if you pass in a string that can't be converted to a Date this will thrown the exception.

所以我们需要断言,这是一个非常简单的例子。另请注意,Excel中的日期有时很难处理,因此您可能会发现需要其他方式转换为数据,例如如果日期是双重的,或者是来自
另一种文化的格式。

So we need assertion, here is a super simple example. Also note, dates coming out of Excel can sometimes be difficult to work with so you may find the need for other ways to convert to a data e.g. if the date comes across as a double or is in a format from another culture.

Dim testDate As Date = Nothing

For Each row As DataGridViewRow In DataGridView1.Rows
    If Date.TryParse(row.Cells("SomeDateColumnName").Value.ToString, testDate) Then
        ' use testDate
    Else
        ' the cell value could not be converted to a date
    End If
Next

另一种解决方案是加载DataGridView检查时数据类型。最后,如果您使用预期列创建了DataTable并定义了数据类型,那么在加载DataTable时,您将对正确的数据类型进行断言。最后,使用
DataTable加载DataGridView。超级简单示例

An alternate solution is when loading the DataGridView check data types. Lastly, if you created a DataTable with expected columns and define the data type then you would do assertions for proper data type when loading the DataTable. In the end you use the DataTable to load the DataGridView. Super simple example

For Each row As DataRow In CType(DataGridView1.DataSource, DataTable).Rows
    ' push data to the database table with the same logic
    ' as you have now
Next


这篇关于vb.net 2013错误 - “无法将参数从字符串转换为日期时间”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆