您现在的位置: 爱51代码网 >> 范文 >> 文章正文
C#字符字串转换成日期出错

儲存數據時出現:從 字元字串转换成日期及/或时间时,转换失败,我是業余愛好新手,求解決方法

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Form1.a1 = True Then

            If ComboBox2.Text = "" Then
                MsgBox("有欄位未填數據")
            Else
                Dim constr As String

                constr = ("Data Source=mark-pc\SQLEXPRESS;Initial Catalog=engineer;Integrated Security=False;User ID=sa;Password=123456;")
                sqlcon.ConnectionString = constr
                sqlcon.Open()

                sqlcmd.CommandText = "select * from mjbm"
                sqlcmd.Connection = sqlcon

                Dim myadapter As New SqlDataAdapter

                myadapter.SelectCommand = sqlcmd
                myadapter.Fill(myds)
                Dim i As Integer = 0
                Dim j As Boolean = False
                For i = 0 To myds.Tables(0).Rows.Count - 1 Step 1
                    If TextBox1.Text = myds.Tables(0).Rows(i)(0) Then
                        j = True
                    End If
                Next
                If j = True Then
                    MsgBox("已有此項了哦!")
                    sqlcon.Close()
                Else
                    sqlcmd.CommandText = "insert into mjbm(類別,編碼,結構,名稱,開模廠商,日期,適用客戶,適用品名,編碼者) values('" & (ComboBox1.Text) & "','" & TextBox1.Text & "','" & ComboBox2.Text & "','" & TextBox2.Text & "','" & ComboBox3.Text & "','" & DateTimePicker1.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & ComboBox4.Text & "')"

                    sqlcmd.Connection = sqlcon
                    sqlcmd.ExecuteNonQuery()
                    If MessageBox.Show("添加成功是否繼續添加", "成功", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) = Windows.Forms.DialogResult.Yes Then

                        Me.Refresh()

                        ComboBox1.Text = ""
                        ComboBox2.Text = ""
                        ComboBox3.Text = ""
                        ComboBox4.Text = ""
                        TextBox1.Text = ""
                        TextBox2.Text = ""
                        TextBox3.Text = ""

                        TextBox4.Text = ""

                        sqlcon.Close()
                    Else

                        sqlcon.Close()
                        Me.Close()
                    End If
                End If
            End If
日期字段你设了datetime类型,插了string类型了吧?
DateTimePicker1.Text=》
转换下
DateTimePicker1.Value.ToString("yyyy-MM-dd hh:MM:ss")
或者 datetimepicker1.customformat="yyyy-MM-dd hh:MM:ss" ;
应该是 DateTimePicker1.Text 的日期格式和数据库日期格式不一致,改用 DateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") 试试

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    maven如何加自定义的包
    redhat 2.6 (santigo 5.6) vsftp
    shell如何实现自动填写操作执行下
    linux shell 文件配置sh:color:
    shell script语法一定要加path吗
    SecureCRT如何访问虚拟机vmWare中
    C#如何读取WINDOWS的放大系数
    cximge的图如何存入数据库并提取
    DBGRID控件显示查询结果文本类型
    TChart控件如何把表中右边的系列
    maven如何加自定义的包
    redhat 2.6 (santigo 5.6) vsftp
    shell如何实现自动填写操作执行下
    linux shell 文件配置sh:color:
    shell script语法一定要加path吗
    SecureCRT如何访问虚拟机vmWare中
    C#如何读取WINDOWS的放大系数
    cximge的图如何存入数据库并提取
    DBGRID控件显示查询结果文本类型
    TChart控件如何把表中右边的系列
    win7x64虚拟机上提示emgu.cv
    .NET Reflector导出"发现不明
    C#如何采集电表数据
    C#两个textBox如何互相控制对
    ExecuteReade如何r创建SqlDa
    如何在ashx页面里面使用Resp
    FormsAuthentication.SetAut
    C#如何读取WINDOWS的放大系数
    C#数据结构和数据类型的区别
    如何实现datagridview数据插
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |