如何向access表中的日期列插入日期数据 (C#实现)
最近在完善 学生宿舍管理系统 中 水电费统计模块的功能时候,涉及到了水电费数据登记日期的记录问题,运行环境是(Win2000+VS2003+Access2000),就此我专做了一个Demo,研究了此问题.
下面的代码向我们演示了如何把一个日期(date)转化成数据库所能识别接受的数据.
![捕捉.jpg](/images/cnblogs_com/finesite/%E6%8D%95%E6%8D%89.jpg)
下面的代码向我们演示了如何把一个日期(date)转化成数据库所能识别接受的数据.
![捕捉.jpg](/images/cnblogs_com/finesite/%E6%8D%95%E6%8D%89.jpg)
1
using System;
2
using System.Drawing;
3
using System.Collections;
4
using System.ComponentModel;
5
using System.Windows.Forms;
6
using System.Data;
7
using System.Data .OleDb ;
8
using System.Globalization ;
9![](/Images/OutliningIndicators/None.gif)
10
namespace SDMS
11
{
12
///
13
/// Form1 的摘要说明。
14
///
15
public class Form1 : System.Windows.Forms.Form
16
{
17
private System.Windows.Forms.Button btnOK;
18
private System.Windows.Forms.Button btnCancel;
19
private System.Windows.Forms.Label labID;
20
private System.Windows.Forms.Label labDate;
21
private System.Windows.Forms.TextBox txtID;
22
private System.Windows.Forms.TextBox txtDate;
23
///
24
/// 必需的设计器变量。
25
///
26
private System.ComponentModel.Container components = null;
27![](/Images/OutliningIndicators/InBlock.gif)
28
public Form1()
29
{
30
//
31
// Windows 窗体设计器支持所必需的
32
//
33
InitializeComponent();
34![](/Images/OutliningIndicators/InBlock.gif)
35
//
36
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
37
//
38
}
39![](/Images/OutliningIndicators/InBlock.gif)
40
///
41
/// 清理所有正在使用的资源。
42
///
43
protected override void Dispose( bool disposing )
44
{
45
if( disposing )
46
{
47
if (components != null)
48
{
49
components.Dispose();
50
}
51
}
52
base.Dispose( disposing );
53
}
54![](/Images/OutliningIndicators/InBlock.gif)
55
Windows 窗体设计器生成的代码
130![](/Images/OutliningIndicators/InBlock.gif)
131
///
132
/// 应用程序的主入口点。
133
///
134
[STAThread]
135
static void Main()
136
{
137
Application.Run(new Form1());
138
}
139![](/Images/OutliningIndicators/InBlock.gif)
140
private void btnOK_Click(object sender, System.EventArgs e)
141
{
142
string str=System.Configuration .ConfigurationSettings .AppSettings ["OleDbConString"];
143
OleDbConnection con=new OleDbConnection (str);
144
try
145
{
146
147
con.Open ();
148
string strInsert = "INSERT INTO DemoTable (ID, DateData) VALUES ( ";
149
150
if((txtID.Text ==string.Empty )||(txtDate.Text ==string.Empty ))
151
{
152
MessageBox.Show ("所需数据不能为空");
153
return;
154
}
155
System.DateTime dt = DateTime.Parse(txtDate.Text,System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN").DateTimeFormat);
156
157![](/Images/OutliningIndicators/InBlock.gif)
158
//转化返回文本框,方便显示
159
int i=int.Parse(txtID.Text);
160
string sNow = "";
161
sNow = dt.ToShortDateString();
162
txtID.Text=i.ToString();
163
txtDate.Text = '#'+sNow+'#';
164
165
strInsert += txtID.Text+", ";
166
strInsert += "CDate("+txtDate.Text+')'+")";
167
OleDbCommand cmd=new OleDbCommand (strInsert,con);
168
cmd.ExecuteNonQuery ();
169![](/Images/OutliningIndicators/InBlock.gif)
170
MessageBox.Show ("添加成功");
171
}
172
catch(Exception err)
173
{
174
throw err;
175
}
176
finally
177
{
178![](/Images/OutliningIndicators/InBlock.gif)
179
con.Close ();
180
}
181
}
182![](/Images/OutliningIndicators/InBlock.gif)
183
private void btnCancel_Click(object sender, System.EventArgs e)
184
{
185
Application.Exit ();
186
}
187
}
188
}
189![](/Images/OutliningIndicators/None.gif)
ps:其中txtDate也可以用DateTimePicker控件来代替,获取它的Value值既可![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/None.gif)
9
![](/Images/OutliningIndicators/None.gif)
10
![](/Images/OutliningIndicators/None.gif)
11
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
25
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](/Images/OutliningIndicators/InBlock.gif)
40
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
130
![](/Images/OutliningIndicators/InBlock.gif)
131
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
132
![](/Images/OutliningIndicators/InBlock.gif)
133
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
134
![](/Images/OutliningIndicators/InBlock.gif)
135
![](/Images/OutliningIndicators/InBlock.gif)
136
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
137
![](/Images/OutliningIndicators/InBlock.gif)
138
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
139
![](/Images/OutliningIndicators/InBlock.gif)
140
![](/Images/OutliningIndicators/InBlock.gif)
141
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
142
![](/Images/OutliningIndicators/InBlock.gif)
143
![](/Images/OutliningIndicators/InBlock.gif)
144
![](/Images/OutliningIndicators/InBlock.gif)
145
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
146
![](/Images/OutliningIndicators/InBlock.gif)
147
![](/Images/OutliningIndicators/InBlock.gif)
148
![](/Images/OutliningIndicators/InBlock.gif)
149
![](/Images/OutliningIndicators/InBlock.gif)
150
![](/Images/OutliningIndicators/InBlock.gif)
151
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
152
![](/Images/OutliningIndicators/InBlock.gif)
153
![](/Images/OutliningIndicators/InBlock.gif)
154
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
155
![](/Images/OutliningIndicators/InBlock.gif)
156
![](/Images/OutliningIndicators/InBlock.gif)
157
![](/Images/OutliningIndicators/InBlock.gif)
158
![](/Images/OutliningIndicators/InBlock.gif)
159
![](/Images/OutliningIndicators/InBlock.gif)
160
![](/Images/OutliningIndicators/InBlock.gif)
161
![](/Images/OutliningIndicators/InBlock.gif)
162
![](/Images/OutliningIndicators/InBlock.gif)
163
![](/Images/OutliningIndicators/InBlock.gif)
164
![](/Images/OutliningIndicators/InBlock.gif)
165
![](/Images/OutliningIndicators/InBlock.gif)
166
![](/Images/OutliningIndicators/InBlock.gif)
167
![](/Images/OutliningIndicators/InBlock.gif)
168
![](/Images/OutliningIndicators/InBlock.gif)
169
![](/Images/OutliningIndicators/InBlock.gif)
170
![](/Images/OutliningIndicators/InBlock.gif)
171
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
172
![](/Images/OutliningIndicators/InBlock.gif)
173
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
174
![](/Images/OutliningIndicators/InBlock.gif)
175
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
176
![](/Images/OutliningIndicators/InBlock.gif)
177
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
178
![](/Images/OutliningIndicators/InBlock.gif)
179
![](/Images/OutliningIndicators/InBlock.gif)
180
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
181
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
182
![](/Images/OutliningIndicators/InBlock.gif)
183
![](/Images/OutliningIndicators/InBlock.gif)
184
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
185
![](/Images/OutliningIndicators/InBlock.gif)
186
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
187
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
188
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
189
![](/Images/OutliningIndicators/None.gif)