如何将DATETIME添加到以下代码中 [英] how to add DATETIME to following code

查看:87
本文介绍了如何将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");
               }
           }
       }

推荐答案

尝试:

Try:
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


您好,

您还可以在SQL中使用
Hi,
You can also use the
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.Now 属性 [ ^ ]。


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

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