如何在C#和窗口编程中使用日期时间选择器在SQL Server 2005中的数据库中插入日期时间. [英] how to insert date time in databse in sql server 2005 using date time picker in c # and window programming .
本文介绍了如何在C#和窗口编程中使用日期时间选择器在SQL Server 2005中的数据库中插入日期时间.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在窗口GUI中的C#中创建一个应用程序,并且我有一个日期时间选择器,我想知道日期时间选择器的值如何存储在数据库表中,其中我的列名日期为"datetime"格式
i am creating an application in c# in window gui and in that i have a date time picker i want know how the value of date time picker is stored in database table in which i have a coloumn name date of ''datetime'' format
推荐答案
您迫切需要学习基础知识.
使用C#读取,插入,更新和删除简单的ADO.NET数据库. [
You seriously need to learn basic things.
Simple ADO.NET Database Read, Insert, Update and Delete using C#.[^]
"Insert into table (id, mytime) values(5, ''" + myDTP.value.ToString("dd-MMM-yyyy HH:mm:ss") + "'')"
您可以在C#或SQL Server端将日期时间转换为数据库格式.
对于C#,请使用DateTime
函数转换为适当的格式.
对于SQL Server,请使用CONVERT
功能 [
You can convert the datetime to the database format either on the C# or on the SQL server side.
For C#, useDateTime
functions to convert to appropriate format.
For SQL Server, use theCONVERT
function[^].
''Try this its very easy
''create table in you database
CREATE TABLE mstemp(
empcode numeric(18, 0) NOT NULL,
fname nvarchar](50) NULL,
lname nvarchar(50) NULL,
doj datetime NULL
)
''write the following code in your aspx.vb page.
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim con As New SqlConnection("Data Source=mydatasource;Initial Catalog=db_emp;Integrated Security=True")
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim cmd As New SqlCommand("insert into mstemp (empcode,doj)values(@empcode,@doj)", con)
cmd.Parameters.AddWithValue("@empcode", 4)
cmd.Parameters.AddWithValue("@doj", DateTimePicker1.Value)
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Saved")
End Sub
End Class
''Enjoy
这篇关于如何在C#和窗口编程中使用日期时间选择器在SQL Server 2005中的数据库中插入日期时间.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文