如何向access表中的日期列插入日期数据 (C#实现)

最近在完善 学生宿舍管理系统 中 水电费统计模块的功能时候,涉及到了水电费数据登记日期的记录问题,运行环境是( Win2000+VS2003+Access2000),就此我专做了一个Demo,研究了此问题.

下面的代码向我们演示了如何把一个日期(date)转化成数据库所能识别接受的数据.
捕捉.jpg
  1 None.gif using  System;
  2 None.gif using  System.Drawing;
  3 None.gif using  System.Collections;
  4 None.gif using  System.ComponentModel;
  5 None.gif using  System.Windows.Forms;
  6 None.gif using  System.Data;
  7 None.gif using  System.Data .OleDb ;
  8 None.gif using  System.Globalization ;
  9 None.gif
 10 None.gif namespace  SDMS
 11 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// 
 13InBlock.gif    /// Form1 的摘要说明。
 14ExpandedSubBlockEnd.gif    /// 

 15InBlock.gif    public class Form1 : System.Windows.Forms.Form
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 17InBlock.gif        private System.Windows.Forms.Button btnOK;
 18InBlock.gif        private System.Windows.Forms.Button btnCancel;
 19InBlock.gif        private System.Windows.Forms.Label labID;
 20InBlock.gif        private System.Windows.Forms.Label labDate;
 21InBlock.gif        private System.Windows.Forms.TextBox txtID;
 22InBlock.gif        private System.Windows.Forms.TextBox txtDate;
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// 
 24InBlock.gif        /// 必需的设计器变量。
 25ExpandedSubBlockEnd.gif        /// 

 26InBlock.gif        private System.ComponentModel.Container components = null;
 27InBlock.gif
 28InBlock.gif        public Form1()
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 30InBlock.gif            //
 31InBlock.gif            // Windows 窗体设计器支持所必需的
 32InBlock.gif            //
 33InBlock.gif            InitializeComponent();
 34InBlock.gif
 35InBlock.gif            //
 36InBlock.gif            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
 37InBlock.gif            //
 38ExpandedSubBlockEnd.gif        }

 39InBlock.gif
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// 
 41InBlock.gif        /// 清理所有正在使用的资源。
 42ExpandedSubBlockEnd.gif        /// 

 43InBlock.gif        protected override void Dispose( bool disposing )
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 45InBlock.gif            if( disposing )
 46ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 47InBlock.gif                if (components != null
 48ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 49InBlock.gif                    components.Dispose();
 50ExpandedSubBlockEnd.gif                }

 51ExpandedSubBlockEnd.gif            }

 52InBlock.gif            base.Dispose( disposing );
 53ExpandedSubBlockEnd.gif        }

 54InBlock.gif
 55ContractedSubBlock.gifExpandedSubBlockStart.gif        Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
 56ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// 
 57InBlock.gif        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
 58InBlock.gif        /// 此方法的内容。
 59ExpandedSubBlockEnd.gif        /// 

 60InBlock.gif        private void InitializeComponent()
 61ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 62InBlock.gif            this.btnOK = new System.Windows.Forms.Button();
 63InBlock.gif            this.btnCancel = new System.Windows.Forms.Button();
 64InBlock.gif            this.labID = new System.Windows.Forms.Label();
 65InBlock.gif            this.labDate = new System.Windows.Forms.Label();
 66InBlock.gif            this.txtID = new System.Windows.Forms.TextBox();
 67InBlock.gif            this.txtDate = new System.Windows.Forms.TextBox();
 68InBlock.gif            this.SuspendLayout();
 69InBlock.gif            // 
 70InBlock.gif            // btnOK
 71InBlock.gif            // 
 72InBlock.gif            this.btnOK.Location = new System.Drawing.Point(56200);
 73InBlock.gif            this.btnOK.Name = "btnOK";
 74InBlock.gif            this.btnOK.TabIndex = 0;
 75InBlock.gif            this.btnOK.Text = "确定";
 76InBlock.gif            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
 77InBlock.gif            // 
 78InBlock.gif            // btnCancel
 79InBlock.gif            // 
 80InBlock.gif            this.btnCancel.Location = new System.Drawing.Point(192200);
 81InBlock.gif            this.btnCancel.Name = "btnCancel";
 82InBlock.gif            this.btnCancel.TabIndex = 1;
 83InBlock.gif            this.btnCancel.Text = "取消";
 84InBlock.gif            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
 85InBlock.gif            // 
 86InBlock.gif            // labID
 87InBlock.gif            // 
 88InBlock.gif            this.labID.Location = new System.Drawing.Point(4040);
 89InBlock.gif            this.labID.Name = "labID";
 90InBlock.gif            this.labID.TabIndex = 2;
 91InBlock.gif            this.labID.Text = "ID";
 92InBlock.gif            // 
 93InBlock.gif            // labDate
 94InBlock.gif            // 
 95InBlock.gif            this.labDate.Location = new System.Drawing.Point(40112);
 96InBlock.gif            this.labDate.Name = "labDate";
 97InBlock.gif            this.labDate.TabIndex = 3;
 98InBlock.gif            this.labDate.Text = "日期";
 99InBlock.gif            // 
100InBlock.gif            // txtID
101InBlock.gif            // 
102InBlock.gif            this.txtID.Location = new System.Drawing.Point(19248);
103InBlock.gif            this.txtID.Name = "txtID";
104InBlock.gif            this.txtID.TabIndex = 4;
105InBlock.gif            this.txtID.Text = "";
106InBlock.gif            // 
107InBlock.gif            // txtDate
108InBlock.gif            // 
109InBlock.gif            this.txtDate.Location = new System.Drawing.Point(192112);
110InBlock.gif            this.txtDate.Name = "txtDate";
111InBlock.gif            this.txtDate.TabIndex = 5;
112InBlock.gif            this.txtDate.Text = "";
113InBlock.gif            // 
114InBlock.gif            // Form1
115InBlock.gif            // 
116InBlock.gif            this.AutoScaleBaseSize = new System.Drawing.Size(614);
117InBlock.gif            this.ClientSize = new System.Drawing.Size(344277);
118InBlock.gif            this.Controls.Add(this.txtDate);
119InBlock.gif            this.Controls.Add(this.txtID);
120InBlock.gif            this.Controls.Add(this.labDate);
121InBlock.gif            this.Controls.Add(this.labID);
122InBlock.gif            this.Controls.Add(this.btnCancel);
123InBlock.gif            this.Controls.Add(this.btnOK);
124InBlock.gif            this.Name = "Form1";
125InBlock.gif            this.Text = "Form1";
126InBlock.gif            this.ResumeLayout(false);
127InBlock.gif
128ExpandedSubBlockEnd.gif        }

129ExpandedSubBlockEnd.gif        #endregion

130InBlock.gif
131ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// 
132InBlock.gif        /// 应用程序的主入口点。
133ExpandedSubBlockEnd.gif        /// 

134InBlock.gif        [STAThread]
135InBlock.gif        static void Main() 
136ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
137InBlock.gif            Application.Run(new Form1());
138ExpandedSubBlockEnd.gif        }

139InBlock.gif
140InBlock.gif        private void btnOK_Click(object sender, System.EventArgs e)
141ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
142InBlock.gif             string  str=System.Configuration .ConfigurationSettings .AppSettings ["OleDbConString"];
143InBlock.gif             OleDbConnection con=new OleDbConnection (str);
144InBlock.gif            try
145ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
146InBlock.gif                
147InBlock.gif                con.Open ();
148InBlock.gif                string strInsert = "INSERT INTO DemoTable (ID, DateData) VALUES ( ";
149InBlock.gif                
150InBlock.gif                if((txtID.Text ==string.Empty )||(txtDate.Text ==string.Empty ))
151ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
152InBlock.gif                    MessageBox.Show ("所需数据不能为空");
153InBlock.gif                    return;
154ExpandedSubBlockEnd.gif                }

155InBlock.gif                System.DateTime dt = DateTime.Parse(txtDate.Text,System.Globalization.CultureInfo.CreateSpecificCulture("zh-CN").DateTimeFormat);
156InBlock.gif                
157InBlock.gif
158InBlock.gif                //转化返回文本框,方便显示
159InBlock.gif                int i=int.Parse(txtID.Text);
160InBlock.gif                string sNow = "";
161InBlock.gif                sNow = dt.ToShortDateString();
162InBlock.gif                txtID.Text=i.ToString();
163InBlock.gif                txtDate.Text = '#'+sNow+'#';
164InBlock.gif            
165InBlock.gif                strInsert += txtID.Text+"";
166InBlock.gif                strInsert += "CDate("+txtDate.Text+')'+")";
167InBlock.gif                OleDbCommand cmd=new OleDbCommand (strInsert,con);
168InBlock.gif                cmd.ExecuteNonQuery ();
169InBlock.gif
170InBlock.gif                MessageBox.Show ("添加成功");
171ExpandedSubBlockEnd.gif            }

172InBlock.gif            catch(Exception err)
173ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
174InBlock.gif                throw err;
175ExpandedSubBlockEnd.gif            }

176InBlock.gif            finally
177ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
178InBlock.gif
179InBlock.gif                con.Close ();
180ExpandedSubBlockEnd.gif            }

181ExpandedSubBlockEnd.gif        }

182InBlock.gif
183InBlock.gif        private void btnCancel_Click(object sender, System.EventArgs e)
184ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
185InBlock.gif            Application.Exit ();
186ExpandedSubBlockEnd.gif        }

187ExpandedSubBlockEnd.gif    }

188ExpandedBlockEnd.gif}

189 None.gif
ps:其中txtDate也可以用DateTimePicker控件来代替,获取它的Value值既可

转载于:https://www.cnblogs.com/finesite/archive/2005/10/18/257124.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值