DateTime插入时出错

时间:2014-03-25 11:13:17

标签: c# winforms datetime

尝试使用c#以编程方式输入DateTime时遇到了下面提到的问题。

错误消息:

  

将nvarchar数据类型转换为日期时间数据类型会导致超出范围的值。

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

    private void button1_Click(object sender, EventArgs e)
    {
        string str = "";

        OpenFileDialog ob = new OpenFileDialog();
        DialogResult dr;
        dr = ob.ShowDialog();
        if (dr == DialogResult.OK)
        {
            str = ob.FileName;

        }

        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();

        con.ConnectionString = @"Data Source=.;Initial Catalog=PicStore;Integrated Security=True";
        cmd.CommandText = "insert into pathstore(paths)values(@path)";
        cmd.Connection = con;

        SqlParameter pa1 = new SqlParameter("@path", DbType.String);
        pa1.Value = str;
        cmd.Parameters.Add(pa1);

        da.InsertCommand = cmd;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

    }

    private void button2_Click(object sender, EventArgs e)
    {

        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();

        con.ConnectionString = @"Data Source=.;Initial Catalog=PicStore;Integrated Security=True";
        cmd.CommandText = "select paths from pathstore where id=@id";
        cmd.Connection = con;

        SqlParameter pa1 = new SqlParameter("@id", DbType.Int32);
        pa1.Value = textBox1.Text;
        cmd.Parameters.Add(pa1);

        da.SelectCommand = cmd;
        con.Open();
        da.Fill(dt);
        string str1 = dt.Rows[0][0].ToString();

        //loading picture into picturebox
        pictureBox1.Load(str1);
        MessageBox.Show(str1);

        //con.Close();
        ///////////////////////////////////////////////////////


        SqlConnection con1 = new SqlConnection();
        SqlCommand cmd1 = new SqlCommand();
        SqlDataAdapter da1 = new SqlDataAdapter();

        con1.ConnectionString = @"Data Source=.;Initial Catalog=PicStore;Integrated Security=True";
        cmd1.CommandText = "insert into DtTimeTable (DTime,id) values (@DTime,@id)";
        cmd1.Connection = con1;


        SqlParameter pa2 = new SqlParameter("@DTime", DbType.DateTime);
        pa2.Value = DateTime.Now.ToString();
        cmd1.Parameters.Add(pa2);

        SqlParameter pa3 = new SqlParameter("@id", DbType.Int32);
        pa3.Value = textBox1.Text;
        cmd1.Parameters.Add(pa3);


        da1.InsertCommand = cmd1;
        con1.Open();
        cmd1.ExecuteNonQuery();
        con1.Close();

    }
}

}

4 个答案:

答案 0 :(得分:4)

这是问题所在:

SqlParameter pa2 = new SqlParameter("@DTime", DbType.DateTime);
pa2.Value = DateTime.Now.ToString();

您已经说过参数是DateTime,但是您将其转换为字符串 - 并且很可能在与服务器期望的不同文化中这样做。 (我怀疑实际导致错误的是什么。)

尽可能避免字符串转换 - 您只需要:

pa2.Value = DateTime.Now;

或者可能:

pa2.Value = DateTime.UtcNow;

请注意,为方便起见,您可以在一行中执行此操作:

cmd1.Parameters.Add("@DTime", SqlDbType.DateTime).Value = DateTime.Now;

答案 1 :(得分:3)

您的参数类型为DateTime。使用DateTime参数代替string

SqlParameter pa2 = new SqlParameter("@DTime", DbType.DateTime);
pa2.Value = DateTime.Now;
cmd1.Parameters.Add(pa2);

答案 2 :(得分:2)

更改此行

 pa2.Value = DateTime.Now.ToString();

 pa2.Value = DateTime.Now;

您正在定义一个DateTime作为其类型的参数,但是您尝试插入一个预期DateTime的字符串。

作为您的代码的旁注。所有初始化SqlDataAdapter并设置其insert命令都没用,因为你直接(就像你应该)调用命令上的ExecuteNonQuery。

我建议在你的一次性物品周围使用Using Statement

using(SqlConnection con = new SqlConnection(... connection string ....))
using(SqlCommand cmd = new SqlCommand(...command text...., con))
{
     con.Open();
     cmd.Parameters.Add(......);
     cmd.ExecuteNonQuery();
}

答案 3 :(得分:1)

问题在于:

SqlParameter pa2 = new SqlParameter("@DTime", DbType.DateTime);
pa2.Value = DateTime.Now.ToString();
cmd1.Parameters.Add(pa2);

在第一行,您将pa定义为DateTime对象,但在下一行,此对象将转换为字符串。

这是正确的代码

SqlParameter pa2 = new SqlParameter("@DTime", DbType.DateTime);
pa2.Value = DateTime.Now;
cmd1.Parameters.Add(pa2);