C# 如何从文本框插入日期到数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9223524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to insert a date from textbox to database
提问by Ajay Mirge
please help me to insert a date from a text box in dd-mm-yyyy format to sql server. my code is as follows:-
请帮助我将日期从 dd-mm-yyyy 格式的文本框中插入到 sql server。我的代码如下:-
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();
and it is throwing an exception saying:- Conversion failed when converting date and/or time from character string.
并且它抛出一个异常说:-从字符串转换日期和/或时间时转换失败。
采纳答案by Pranay Rana
You need to convert data according to you sql server formate that way you can resolve issue ..
您需要根据您的 sql server 格式转换数据,这样您就可以解决问题..
Try
尝试
String UrDate = "27/12/2011";
System.Globalization.DateTimeFormatInfo dateInfo = new System.Globalization.DateTimeFormatInfo();
dateInfo.ShortDatePattern = "dd/MM/yyyy";
DateTime validDate= Convert.ToDateTime(toDate, dateInfo);
or
或者
// 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);
Make use of Paramerize query to avoid SQL INJECTION...make code less error pron Walkthrough: Displaying Data in a Windows Form Using a Parameterized Query
利用参数化查询来避免 SQL 注入...使代码减少错误pron 演练:使用参数化查询在 Windows 窗体中显示数据
回答by FarligOpptreden
Just a word of caution - you need to sanitize that query to prevent SQL injection attacks. Consider using parameterised queries. Read up about it, it's not really the scope of this answer.
提醒一下 - 您需要清理该查询以防止 SQL 注入攻击。考虑使用参数化查询。仔细阅读它,这不是这个答案的真正范围。
You should create strongly typed DateTime objects first and then format them the way you need to insert. Consider the following modification to your code:
您应该首先创建强类型的 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();
EDIT
编辑
I removed the string parameter from the ToString() so you can get a valid DateTime string that's usable by SQL Server.
我从 ToString() 中删除了字符串参数,以便您可以获得 SQL Server 可用的有效 DateTime 字符串。
回答by Anand Zambre
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();

