SqlConnection con = new SqlConnection("Data Source=RANJEETMAURYA;Initial Catalog=Project;Integrated Security=True");
con.Open();
DateTime current = DateTime.Now;
//DateTime CurrentDate;
//CurrentDate = Convert.ToDateTime(DateTime.Now.ToString("dd-MMM-yyyy"));
current = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy hh:mm"));
SqlCommand cmd = new SqlCommand(@"INSERT INTO CustomerDetails
(Date, Name, Gender, Address, Contact_No, Email_ID)
VALUES ('" +current+ "','" + txtName.Text + "','" + Gender + "','" + txtAddress.Text + "','" + txtContact.Text + "','" + txtEmail.Text + "')", con);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Customer Information Added Successfully.", "Dairy Management System", MessageBoxButtons.OK, MessageBoxIcon.Information);
SQLFunctions.Refresh(this.dataGridCustomerDetails);
这是错误请帮帮我 出于什么原因它运行了几次,它没有运行一段时间。
System.FormatException was unhandled
HResult=-2146233033
Message=String was not recognized as a valid DateTime.
Source=mscorlib
StackTrace:
at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
at System.Convert.ToDateTime(String value)
at IndianDiary.frmCustomerDetails.btnAddNew_Click(Object sender, EventArgs e) in
答案 0 :(得分:4)
您正在将当前时间转换为字符串,然后将字符串解析回DateTime:
DateTime current = DateTime.Now;
current = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy hh:mm"));
这有什么意义?只需使用DateTime.Now
即可。还可以使用命令参数。
string sql = @"INSERT INTO CustomerDetails
(Date, Name, Gender, Address, Contact_No, Email_ID)
VALUES (@date, @name, @gender, @address, @contactNo, @emailId)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@date", DateTime.Now);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@gender", Gender);
cmd.Parameters.AddWithValue("@address", txtAddress.Text);
cmd.Parameters.AddWithValue("@contactNo", txtContact.Text);
cmd.Parameters.AddWithValue("@emailId", txtEmail.Text);
请参阅How does SQLParameter prevent SQL Injection?
还可以使用 App.config 来存储连接字符串:
<connectionStrings>
<add name="ranjeet"
connectionString="Data Source=RANJEETMAURYA;Initial Catalog=Project;Integrated Security=True"
providerName="System.Data.EntityClient" />
</connectionStrings>
然后您就可以使用ConfigurationManager
获得它。
还将连接和命令包装到using
语句中以自动处理它们:
using (SqlConnection con = new SqlConnection(connectionString))
using (SqlCommand cmd = new SqlCommand(sql, con))
{
// add parameters
con.Open();
cmd.ExecuteNonQuery();
}
答案 1 :(得分:0)
为什么要将它转换为字符串只是将DateTime.Now发送到数据库并从数据库中检索它时使用此
retrieveDate = DateRetrieved.ToString(“MM / dd / yyyy hh:mm”);
答案 2 :(得分:0)
参考Sergey Berezovskiy
的答案
您也可以将参数传递为:
string sql = @"INSERT INTO CustomerDetails
(Date, Name, Gender, Address, Contact_No, Email_ID)
VALUES (@date, @name, @gender, @address, @contactNo, @emailId)";
SqlCommand cmd = new SqlCommand(sql);
cmd.Parameters.Add("@date", SqlDbType.Date).Value = DateTime.Now;
cmd.Parameters.Add("@name", SqlDbType.Varchar, 50).Value = txtName.Text;
cmd.Parameters.Add("@gender", SqlDbType.Varchar, 10).Value = Gender;
cmd.Parameters.Add("@address", SqlDbType.Varchar, 50).Value =txtAddress.Text;
cmd.Parameters.Add("@contactNo", SqlDbType.Varchar, 25).Value = txtContact.Text;
cmd.Parameters.Add("@emailId", SqlDbType.Varchar, 35).Value =txtEmail.Text;