使用c#插入日期.Net到SQL Server 2008使用插入查询

本文关键字:插入 2008 Server 查询 日期 Net 使用 SQL | 更新日期: 2023-09-27 17:53:44

我正在使用c#制作桌面应用程序。. NET和SQL Server 2008.......其中,我取了一个标签名为date,并有一个工具箱日期时间选择器。并在数据库...........中取了字段名日期现在我想通过表单插入日期使用datetimepicker并插入到数据库.......

使用代码

SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=PSM");
con.Open();
SqlCommand cmd = new SqlCommand("insert into Campaign(Campaign_id,List_of_thresholds,Duration,Starting_Date,Ending_Date,Total_Budget_of_all_thresholds) values (" + Convert.ToInt32(cbocampaign.Text) + ",'" + cbolist_threshold.Text + "',' " + txtduration.Text + " '," + Convert.ToDateTime(DateTimePicker1.Text) + "," + Convert.ToDateTime(DateTimePicker2.Text) + "," + Convert.ToInt32(txtbudget.Text) + ")", con);
            cmd.ExecuteNonQuery();
MessageBox.Show("Insertion successfully done");

*问题:*无法转换发生日期时间选择器错误.....

Convert.ToDateTime(DateTimePicker1.Text) `

请给我解决这个问题,也给我使用数据网格视图获取数据到文本框的编码

shivani

使用c#插入日期.Net到SQL Server 2008使用插入查询

使用SqlParameter而不是执行字符串,它将解决您的问题,并防止sql注入

我认为问题是在默认的日期时间格式。您可以尝试使用datetime来解析datetime。ParseExact http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx。

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

此外,我同意ArsenMkrt的观点,最好使用参数