使用c#向数据库中添加数据时,cmd.ExecuteNonQuery();附近总是报错,代码如下:
voidAdd()//添加函数{if(txt_Pno.Text!=""){if(conn.State!=ConnectionState.Open){conn.Open()...
void Add() //添加函数
{
if (txt_Pno.Text != "")
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.CommandType = CommandType.Text;
string Pno = txt_Pno.Text;
string Pname = txt_Pname.Text;
string Pzyno = txt_Pzyno.Text;
DateTime csrq = DateTime.Parse(dtp_Pcsrq.Text);
//txt_Page.Text = (DateTime.Now.Year - DateTime.Parse(dtp_Prysj.Text).Year).ToString();
int age = int.Parse(txt_Page.Text);
string sex = txt_Psex.Text;
string phonenum = txt_Plxfs.Text;
DateTime rytime = DateTime.Parse(dtp_Prysj.Text);
DateTime cytime = DateTime.Parse(dtp_Prysj.Text);
string Pryzd = cmb_Pryzd.Text;
string Pcyzd = cmb_Pcyzd.Text;
string Pbqzg = txt_Pbqzg.Text;
float Pzfy = float.Parse(txt_Pzfy.Text);
float Pzyf = float.Parse(txt_Pzyf.Text);
float Pxyf = float.Parse(txt_Pxyf.Text);
float Pqtf = float.Parse(txt_Pqtf.Text);
//float Pyzb = float.Parse(txt_Pyzb.Text);
string Pcwys = txt_Pcwys.Text;
string sql = "insert into medicalRecord(病人ID,病人姓名,病人住院号,出生日期,年龄,性别,联系方式,病人入院时间,病人出院时间,病人入院诊断,病人出院诊断,病人病情转归,病人总费用,病人西药费,病人中药费,病人其他费用,床位医生) values (" + "'" + Pno.Trim() + "'" + ","
+ "'" + Pname.Trim() + "'" + ","
+ "'" + Pzyno.Trim() + "'"+ ","
+ csrq + ","
+ age + ","
+ "'" + sex.Trim() + "'" + ","
+ "'" + phonenum.Trim() + "'" + ","
+ rytime + ","
+ cytime + ","
+"'" + Pryzd.Trim() + "'"+ ","
+"'" + Pcyzd.Trim() + "'"+","
+"'" + Pbqzg.Trim() + "'"+","
+Pzfy +","
+ Pzyf +","
+Pxyf+","
+Pqtf+","
//+ Pyzb +","
+"'" + Pcwys.Trim() + "')";
cmd.CommandText = sql;
cmd.Connection = conn;
DA.InsertCommand = cmd;
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("添加病历成功!", "操作提示");
//Refresh();
}
else
{
MessageBox.Show("病人ID不能为空!", "操作提示");
return;
}
} 展开
{
if (txt_Pno.Text != "")
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.CommandType = CommandType.Text;
string Pno = txt_Pno.Text;
string Pname = txt_Pname.Text;
string Pzyno = txt_Pzyno.Text;
DateTime csrq = DateTime.Parse(dtp_Pcsrq.Text);
//txt_Page.Text = (DateTime.Now.Year - DateTime.Parse(dtp_Prysj.Text).Year).ToString();
int age = int.Parse(txt_Page.Text);
string sex = txt_Psex.Text;
string phonenum = txt_Plxfs.Text;
DateTime rytime = DateTime.Parse(dtp_Prysj.Text);
DateTime cytime = DateTime.Parse(dtp_Prysj.Text);
string Pryzd = cmb_Pryzd.Text;
string Pcyzd = cmb_Pcyzd.Text;
string Pbqzg = txt_Pbqzg.Text;
float Pzfy = float.Parse(txt_Pzfy.Text);
float Pzyf = float.Parse(txt_Pzyf.Text);
float Pxyf = float.Parse(txt_Pxyf.Text);
float Pqtf = float.Parse(txt_Pqtf.Text);
//float Pyzb = float.Parse(txt_Pyzb.Text);
string Pcwys = txt_Pcwys.Text;
string sql = "insert into medicalRecord(病人ID,病人姓名,病人住院号,出生日期,年龄,性别,联系方式,病人入院时间,病人出院时间,病人入院诊断,病人出院诊断,病人病情转归,病人总费用,病人西药费,病人中药费,病人其他费用,床位医生) values (" + "'" + Pno.Trim() + "'" + ","
+ "'" + Pname.Trim() + "'" + ","
+ "'" + Pzyno.Trim() + "'"+ ","
+ csrq + ","
+ age + ","
+ "'" + sex.Trim() + "'" + ","
+ "'" + phonenum.Trim() + "'" + ","
+ rytime + ","
+ cytime + ","
+"'" + Pryzd.Trim() + "'"+ ","
+"'" + Pcyzd.Trim() + "'"+","
+"'" + Pbqzg.Trim() + "'"+","
+Pzfy +","
+ Pzyf +","
+Pxyf+","
+Pqtf+","
//+ Pyzb +","
+"'" + Pcwys.Trim() + "')";
cmd.CommandText = sql;
cmd.Connection = conn;
DA.InsertCommand = cmd;
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("添加病历成功!", "操作提示");
//Refresh();
}
else
{
MessageBox.Show("病人ID不能为空!", "操作提示");
return;
}
} 展开
5个回答
展开全部
你的sql语句拼写的有问题(导致的sql本身语法的错误),你用断点跟踪一下,看看在拼写sql语句的时候格式转化,还有单引号的问题上是不是出了问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
时间日期插入也要求引号,和字符串一样,格式要匹配 如yyyy-MM-dd hh:mm:ss
肯定是SQL语句字符串串联错了 ,
肯定是SQL语句字符串串联错了 ,
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-02-25
展开全部
强烈建议断点调试 这是一个好习惯 以后遇到问题不需要求助 对自己提升也有好处
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议打个断点跟踪下
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询