本文介绍了如何将DATETIME添加到以下代码中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码添加所有类型的文档,图片等..但现在我需要添加日期时间,在该时间文件添加日期和时间.....



this is my code to add all kind of doc,pic,etc.. but now i need to add datetime at which time file added that date and time.....

private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
       {
           if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {

               FileInfo fi = new FileInfo(openFileDialog1.FileName);
               byte[] data = new byte[fi.Length];
               //DateTime dt = fi.CreationTime;

               FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
               fs.Position = 0;
               fs.Read(data, 0, Convert.ToInt32(fi.Length));
               SqlConnection conn = new SqlConnection(string.Format("Data Source=SELVA-T;Initial Catalog=textile;Integrated Security=True"
                       , ServerName
                       , DatabaseName)
                       );

               try
               {
                   conn.Open();
                   SqlCommand cmd = new SqlCommand("INSERT INTO bill ([Bill Name],[Data]) "
                       + string.Format("values( '{0}',@Data )"
                           , fi.Name), conn);
                   SqlParameter pdata = new SqlParameter("@Data", SqlDbType.Image);
                   pdata.Direction = ParameterDirection.Input;
                   pdata.SqlValue = data;
                   cmd.Parameters.Add(pdata);
                   cmd.ExecuteNonQuery();
                   long si;
                   si = fi.Length / 1024;
                   MessageBox.Show("u inserted the file size of " + si.ToString() + "kb");
                   textileDataSet.Tables[0].Rows.Add(fi.Name,data);
                   textileDataSet.Tables[0].AcceptChanges();
                   conn.Close();
               }
               catch (Exception)
               {
                   MessageBox.Show("both r same file");
               }
           }
       }

推荐答案

SqlCommand cmd = new SqlCommand("INSERT INTO bill ([Bill Name],[Data],Date1) VALUES (@File,@Data, @Date)", conn);
cmd.Parameters.AddWithValue("@File", fi.FileName);
cmd.Parameters.AddWithValue("@Data", pdata);
cmd.Parameters.AddWithValue("@Date", DateTime.Now);
cmd.ExecuteNonQuery();





忘记双引号! :O



Forgot the double quotes! :O


GetDate()

。 br $>


in SQL.

INSERT INTO bill (FileAddedTime,[Bill Name],[Data]) "
                        + string.Format("values( GetDate(), '{0}',@Data )" , fi.Name), conn);



最好的问候

Muthuraja


Best Regards
Muthuraja



这篇关于如何将DATETIME添加到以下代码中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:06