2012-02-10 47 views
3

请帮我插入一个日期从dd-mm-yyyy格式的文本框到sql服务器。 我的代码如下: -如何插入日期从文本框到数据库

 int prio = Convert.ToInt32(Priority.Text); 
     string stdate = planstart.Text; 
     string endate= planend.Text; 

     string actst = actualstart.Text; 
     string acten = actualend.Text; 



      SqlConnection myconnection = new SqlConnection(constring); 
      SqlCommand mycommand = new SqlCommand(); 
      DataSet mydataset = new DataSet(); 
      SqlDataAdapter mydataadapter = new SqlDataAdapter(); 

      myconnection.Open(); 
      mycommand.Connection = myconnection; 
      mycommand.CommandText = " insert into project_status.dbo.Project_Status_Report values('" + projectcode.Text + "','" + projectname.Text + "',(select P_Code from project_status.dbo.Project_Type where Project_Type = '" + projecttype.Text + "')," + prio + ",'" + stdate + "','" + endate + "','" + actst + "','" + acten + "','" + currentstatus.Text + "','" + remark.Text + "','no');"; 

      mycommand.CommandType = CommandType.Text; 
      mycommand.ExecuteNonQuery(); 

,它是抛出一个异常说: - 从字符串转换日期和/或时间时 转换失败。

+1

1)查看**参数化查询**以防止SQL注入。 2)如果你的日期应该是一个日期,不要把它当作一个字符串。验证它是一个有效的日期,将其转换,然后将其作为查询中的参数提供。 – 2012-02-10 06:15:33

+0

你能告诉我如何转换它吗?因为我已经尝试过但未能转换。 – 2012-02-10 06:19:09

+0

如果它的工作比接受答案................. – 2012-02-10 06:55:32

回答

1

您需要将数据转换根据您的SQL Server甲这样就可以解决问题..

尝试

String UrDate = "27/12/2011"; 
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo(); 
dateInfo.ShortDatePattern = "dd/MM/yyyy"; 
DateTime validDate= Convert.ToDateTime(toDate, dateInfo); 

Format String For Dates

// String to DateTime 
String MyString; 
MyString = "1999-09-01 21:34 PM"; 
//MyString = "1999-09-01 21:34 p.m."; //Depends on your regional settings 

DateTime MyDateTime; 
MyDateTime = new DateTime(); 
MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", 
             null); 

制作使用Paramerize查询来一个void SQL INJECTION ...使代码更少错误代码 Walkthrough: Displaying Data in a Windows Form Using a Parameterized Query

+0

实际上我采取日期的格式dd/mm/yyyy.and我已经尝试了上面的代码,但它是抛出一个异常说,字符串是不是在有效的日期fomat.as我是新的,我不知道这些东西,请帮助我。谢谢你.. – 2012-02-10 06:23:52

+0

尝试编辑答案..... – 2012-02-10 06:27:53

+0

谢谢你,为我工作.. – 2012-02-10 06:51:30

0

只需谨慎 - 您需要清理该查询以防止SQL注入攻击。考虑使用参数化查询。阅读它,这不是这个答案的范围。

您应该首先创建强类型的DateTime对象,然后按照需要插入的方式对其进行格式化。考虑以下修改代码:

string stdate = DateTime.Parse(planstart.Text).ToString(); 
string endate = DateTime.Parse(planend.Text).ToString(); 

string actst = DateTime.Parse(actualstart.Text).ToString(); 
string acten = DateTime.Parse(actualend.Text).ToString(); 

编辑

我从toString()方法去除字符串参数,这样就可以得到有效的DateTime字符串,这是由SQL Server使用。

+0

谢谢you.but我仍然收到错误:-String不被认为是一个有效的日期时间。 – 2012-02-10 06:26:06

+0

那可能是数据库的区域设置。如果数据类型是日期时间,则需要实际通过VALID日期时间对象。它的显示格式取决于区域设置。考虑从ToString()方法中删除字符串参数 - 它应考虑区域设置并返回一个有效的DateTime字符串。 – FarligOpptreden 2012-02-10 06:28:55

0
con.Open(); 
string query = "insert_demo"; 
    /* date fromat Stored*/ 
TextBox2.Text = DateTime.Now.ToLongDateString(); 
SqlCommand com = new SqlCommand(query, con); 


com.CommandType = CommandType.StoredProcedure; 

com.Parameters.AddWithValue("@Name", TextBox1.Text.ToString()); 
com.Parameters.AddWithValue("@Date", TextBox2.Text.ToString()); 

com.ExecuteNonQuery();