如何使用C#.net在SQL Server 2005中插入日期? [英] how to insert date in SQL server 2005 using C# .net?

查看:42
本文介绍了如何使用C#.net在SQL Server 2005中插入日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我....此代码出现问题...
在我的数据库中,有一个包含2列的表,其数据类型如下
名称varchar(50)
日期datetime

plz help me....m getting problem with this code...
in my database there is a table with 2 columns and their data types are as follows
name varchar(50)
date datetime

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection("Data Source=AHSAN-PC\\SQLEXPRESS; Initial Catalog=waf; Integrated Security=SSPI;");
                con.Open();
                MessageBox.Show("Connection Established Successfully with Database", "Database Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SqlCommand cmd = new SqlCommand("insert into loginfo values('" + textBox1.Text + "',  ' " + dateTimePicker1.Value.ToShortDateString()+ " ' ", con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Insertion successfully done");
            }
            catch (Exception e1)
            {
                MessageBox.Show("error:" + e1.Message);
            }

        }
    }
}

推荐答案

我认为您要插入数据库的价值形式存在问题.
使用dateTimePicker1.Value代替
dateTimePicker1.Value.ToShortDateString().

使用
Convert.ToDateTime(dateTimePicker1.Value.ToShortDateString());
I think problem in the formate of value that you are inserting in database.
use dateTimePicker1.Value instead of
dateTimePicker1.Value.ToShortDateString().
or
use
Convert.ToDateTime(dateTimePicker1.Value.ToShortDateString());


删除日期周围的空格:

Remove the spaces around the date:

SqlCommand cmd = new SqlCommand("insert into loginfo values('" + textBox1.Text + "',  '" + dateTimePicker1.Value.ToShortDateString()+ "' ", con);



如果仍然有问题,请尝试以下方法:



Try this if it still has problems :

dateTimePicker1.Value.ToString("yyyy-MM-dd")


SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conm"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter adp = new SqlDataAdapter();
    DataSet ds = new DataSet();

 try
        {


 cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;
            // cmd.CommandText = "update ReglaCategorizacion set Ordenejecucion='" + lblOExecution.Text + "'where IdRegla='" + lblPreviousRegla.Text + "' ";
  cmd.CommandText = "insert into UserDetail(RoleID,FirstName,LastName)";
            cmd.Parameters.AddWithValue("@RoleID", Rdb_UserType.SelectedValue);
            cmd.Parameters.AddWithValue("@FirstName", txt_name.Text);
            cmd.Parameters.AddWithValue("@LastName", txt_Surname.Text);
           
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Redirect("~/ErroPage.aspx?a=" + ex.Message);

        }


这篇关于如何使用C#.net在SQL Server 2005中插入日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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