【发布时间】:2014-08-27 22:56:42
【问题描述】:
string c = "";
string s = "";
string d = "";
string t = "";
if (CarNameCombo.SelectedIndex >= 0 && SourceCombo.SelectedIndex >= 0 && DestinationCombo.SelectedIndex >= 0 && NumberOfPassengers.SelectedIndex >= 0)
c = CarNameCombo.Items[CarNameCombo.SelectedIndex].ToString();
s = SourceCombo.Items[SourceCombo.SelectedIndex].ToString();
d = SourceCombo.Items[DestinationCombo.SelectedIndex].ToString();
t = NumberOfPassengers.Items[NumberOfPassengers.SelectedIndex].ToString();
MessageBox.Show(""+c+s+d+t);
string date = dateTimePicker1.Text;
string date1 = dateTimePicker2.Text;
string x = richTextBox1.Text;
string y = richTextBox2.Text;
MessageBox.Show("" +date +date1);
SqlConnection conn = new SqlConnection("Data Source=PRAVEEN\\SQLEXPRESS;Initial Catalog=travelbooking;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into BookDetails(CarName,Source,Destination,Date,FromAddress,ToAddress,Time,Numberpassengers)VALUES('" + c + "','" + s + "','" + d + "','" + date + "','" + x + "','" + y + "' '"+ date1 + "','" + t + "'", conn)");
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
//cmd.Parameters.AddWithValue("@CarName", c);
//cmd.Parameters.AddWithValue("@Source", s);
//cmd.Parameters.AddWithValue("@Destination", d);
//cmd.Parameters.AddWithValue("@Date", date);
//cmd.Parameters.AddWithValue("@FromAddress", richTextBox1.Text);
//cmd.Parameters.AddWithValue("@ToAddress", richTextBox2.Text);
//cmd.Parameters.AddWithValue("@Time", date1);
//cmd.Parameters.AddWithValue("@Numberpassengers", t);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
我正在使用 c# 和 ado 在 Windows 窗体项目中工作。我得到并抛出异常说无法将日期和/或时间从字符转换为字符串,我将数据库中的数据类型指定为时间(7)并将日期指定为日期......我需要做一些额外的事情吗?这是消息“从字符串转换日期和/或时间时转换失败”标签sql异常未处理。
【问题讨论】:
-
您应该发布您收到的整个错误消息。还要将您正在使用的平台添加到标签列表中,否则有知识帮助您的人不会看到问题。
标签: c# datetime exception ado.net insertion