最近在完善 学生宿舍管理系统 中 水电费统计模块的功能时候,涉及到了水电费数据登记日期的记录问题,运行环境是(
Win2000+VS2003+Access2000),就此我专做了一个Demo,研究了此问题.
下面的代码向我们演示了如何把一个日期(date)转化成数据库所能识别接受的数据.
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
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
28
public Form1()
29
{
30
//
31
// Windows 窗体设计器支持所必需的
32
//
33
InitializeComponent();
34
35
//
36
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
37
//
38
}
39
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
55
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
56
/**////
57
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
58
/// 此方法的内容。
59
///
60
private void InitializeComponent()
61
{
62
this.btnOK = new System.Windows.Forms.Button();
63
this.btnCancel = new System.Windows.Forms.Button();
64
this.labID = new System.Windows.Forms.Label();
65
this.labDate = new System.Windows.Forms.Label();
66
this.txtID = new System.Windows.Forms.TextBox();
67
this.txtDate = new System.Windows.Forms.TextBox();
68
this.SuspendLayout();
69
//
70
// btnOK
71
//
72
this.btnOK.Location = new System.Drawing.Point(56, 200);
73
this.btnOK.Name = "btnOK";
74
this.btnOK.TabIndex = 0;
75
this.btnOK.Text = "确定";
76
this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
77
//
78
// btnCancel
79
//
80
this.btnCancel.Location = new System.Drawing.Point(192, 200);
81
this.btnCancel.Name = "btnCancel";
82
this.btnCancel.TabIndex = 1;
83
this.btnCancel.Text = "取消";
84
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
85
//
86
// labID
87
//
88
this.labID.Location = new System.Drawing.Point(40, 40);
89
this.labID.Name = "labID";
90
this.labID.TabIndex = 2;
91
this.labID.Text = "ID";
92
//
93
// labDate
94
//
95
this.labDate.Location = new System.Drawing.Point(40, 112);
96
this.labDate.Name = "labDate";
97
this.labDate.TabIndex = 3;
98
this.labDate.Text = "日期";
99
//
100
// txtID
101
//
102
this.txtID.Location = new System.Drawing.Point(192, 48);
103
this.txtID.Name = "txtID";
104
this.txtID.TabIndex = 4;
105
this.txtID.Text = "";
106
//
107
// txtDate
108
//
109
this.txtDate.Location = new System.Drawing.Point(192, 112);
110
this.txtDate.Name = "txtDate";
111
this.txtDate.TabIndex = 5;
112
this.txtDate.Text = "";
113
//
114
// Form1
115
//
116
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
117
this.ClientSize = new System.Drawing.Size(344, 277);
118
this.Controls.Add(this.txtDate);
119
this.Controls.Add(this.txtID);
120
this.Controls.Add(this.labDate);
121
this.Controls.Add(this.labID);
122
this.Controls.Add(this.btnCancel);
123
this.Controls.Add(this.btnOK);
124
this.Name = "Form1";
125
this.Text = "Form1";
126
this.ResumeLayout(false);
127
128
}
129
#endregion
130
131
/**////
132
/// 应用程序的主入口点。
133
///
134
[STAThread]
135
static void Main()
136
{
137
Application.Run(new Form1());
138
}
139
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
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
170
MessageBox.Show ("添加成功");
171
}
172
catch(Exception err)
173
{
174
throw err;
175
}
176
finally
177
{
178
179
con.Close ();
180
}
181
}
182
183
private void btnCancel_Click(object sender, System.EventArgs e)
184
{
185
Application.Exit ();
186
}
187
}
188
}
189
ps:其中txtDate也可以用DateTimePicker控件来代替,获取它的Value值既可
posted @ 2005-10-18 13:02
Kevin Li 阅读(2172)
评论(3) 编辑 收藏 所属分类:
外文翻译