c#从字符串向datetime转换时失败
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace xlgl2
{
public partial class frmAdd : Form
{
public frmAdd()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string studentSex = "";
if (optMale.Checked)
{
studentSex = "男";
}
if (optFemale.Checked)
{
studentSex = "女";
}
string sql = "insert into StudentInformation(StuID,StuName,StuSex,StuNativePlace,StuBirthDate) Values('" + mtxtID.Text + "','"+txtName.Text.Trim()+"','"+studentSex+"','"+cbNativePlace.SelectedItem+"','"+dateTimePicker1.Value+"')";
if(OperateDB.ExecuteNonQuery(sql)==1)
{
MessageBox.Show("数据添加成功!");
}
else
{
MessageBox.Show("数据添加失败!");
}
}
}
} 运行时提示异常,求大神指导 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace xlgl2
{
public partial class frmAdd : Form
{
public frmAdd()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
string studentSex = "";
if (optMale.Checked)
{
studentSex = "男";
}
if (optFemale.Checked)
{
studentSex = "女";
}
string sql = "insert into StudentInformation(StuID,StuName,StuSex,StuNativePlace,StuBirthDate) Values('" + mtxtID.Text + "','"+txtName.Text.Trim()+"','"+studentSex+"','"+cbNativePlace.SelectedItem+"','"+dateTimePicker1.Value+"')";
if(OperateDB.ExecuteNonQuery(sql)==1)
{
MessageBox.Show("数据添加成功!");
}
else
{
MessageBox.Show("数据添加失败!");
}
}
}
} 运行时提示异常,求大神指导 展开
3个回答
展开全部
以后凡是这类问题,在执行前(具体到上面就是if(OperateDB.ExecuteNonQuery(sql)==1))那一行那下断点,运行,然后检查sql的值是什么,如果还是看不出问题,把这个sql贴到sql server管理器里去运行。
追问
能详细说说吗?看不懂
本回答被网友采纳
展开全部
用 SqlParameter,不要用字符串连接
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询