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窗体项目中工作。我得到和异常抛出说不能将日期和/或时间从字符转换为字符串,我在我的数据库中指定数据类型为time(7),日期为date ....我需要做一些额外的事情吗???这是meesage"从字符串转换日期和/或时间时转换失败"在标签sql异常下未处理。
答案 0 :(得分:1)
找到这个
namespace First_Csharp_app
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String gender; //we have to define this
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(str);
String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
String query = "insert into data (E.id,name,surname,age,gender,DOB) values ('"+this.eid_txt.text+"','"+this.nametxt.text+"','"+this.surname_txt.text+"','"+this.age_txt.text+"' , '"+this.gender+"' , '"+this.DateTimePicker1.Text+"')";
SqlCommand cmd = new sqComamnd(query,con);
SqlDataReader dbr;
try
{
con.open();
dbr = cmd.ExecuteReader();
MessageBox.Show("saved");
while(dbr.read())
{
}
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}
private void rediobutton1.checked(object sender, EventArgs e)
{
gender = "male";
}
private void rediobutton1.checked(object sender, EventArgs e)
{
gender = "female";
}
}
}
答案 1 :(得分:0)
使用DateTimePicker.Value代替DateTimePicker.Text并创建一个接受此DateTime
值而不是文本字符串的参数化查询,例如:
DateTime date = dateTimePicker1.Value;
DateTime date1 = dateTimePicker2.Value;
...
SqlCommand cmd = new SqlCommand(
"insert into BookDetails(CarName,Source,Destination,Date,FromAddress,ToAddress,Time,Numberpassengers) " +
" VALUES(@CarName,@Source,@Destination,@Date,@FromAddress,@ToAddress, " +
" @Time,@NumPassengers)", 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);
原始代码失败,因为您传递了以任意格式而不是实际日期值格式化的日期。 SQL Server尝试使用与最终用户的区域设置不匹配的列的排序规则来解释此字符串。