如何使用vb.net在sql DB中插入date\time [英] How to insert date\time in sql DB using vb.net

查看:83
本文介绍了如何使用vb.net在sql DB中插入date\time的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我是vb.net的新手,我正在尝试在我的表单中使用日期时间选择器。

在datetime列中向sql server插入日期我收到一条错误,指出它无法将字符串转换为日期。



我理解我正在尝试插入字符串的事实进入日期列,因此请帮助我正确的代码。这是我正在使用的代码。



Hi,

I am new to vb.net and I am trying to use a datetime picker in my form.
While inserting date to the sql server in datetime column I am getting an error stating it cannot convert the string to date.

I understand the fact that I am trying to insert string into date column, hence please help me with the right code. Here is the code I am using.

Dim sqlCmdStr_Insrt As String = "INSERT INTO Project_details(project_Id,Project_name,Description," _
        & "Stdate,NdDate)Values('" & txtPro_id.Text & "','" & txtPro_Nm.Text & "','" & txtPro_Dsc.Text & "','" _
        & dateSt_Projct.Value.ToLongDateString & "','" & dateEnd_Project.Value.ToLongDateString & "')"

Try
            'Executing the Sql Query to insert the data to the Project_details table
            DataStore.ExecuteNonQuery(sqlCmdStr_Insrt, sqlConnStr)
            Call btnReset_CrtPro_Click(sender, e)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

推荐答案

Imports System.Data.SqlClient
Public Class Form6
    Dim cmd As New SqlCommand
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            cmd.Connection = Class1.strcon
            cmd.CommandText = "insert into dt values('" & (TextBox1.Text) & "')"
            cmd.ExecuteNonQuery()
            MessageBox.Show("inserted")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = System.DateTime.Now.ToString((" yyyy-MM-dd"))
    End Sub
End Class


你的代码完全糟透了。如果您是VB.NET的新手,您应该学习它。然后学习SQL。但是,因为我确定你因为这个烂摊子而受到报酬并且不关心成为一名优秀的程序员,所以我们假设你不会这样做,对吧?



你的代码是一个灾难bc你是字符串mashing SQL。这意味着任何想要的用户都可以删除您的数据库。阅读SQL注入。如果您使用参数化查询修复SQL注入问题,副作用将是解决您的DateTime问题。
Your code totally sucks. If you're new to VB.NET, you should learn it,. THEN learn SQL. However, as I'm sure you're being paid for this mess and don't care about being a good programmer, we'll assume you won't do that, right ?

Your code is a disaster b.c you're string mashing SQL. Which means any user who wants to, can erase your database. Read up on SQL injection. If you fix your SQL injection issues by using parameterised queries, a side effect will be to solve your DateTime issue.


这篇关于如何使用vb.net在sql DB中插入date\time的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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