永发信息网

C#添加数据到数据库,代码应该如何添加呢?

答案:4  悬赏:30  手机版
解决时间 2021-04-23 15:04
  • 提问者网友:感性作祟
  • 2021-04-23 08:54

private string GetInsertData()
{
string tx = string.Empty;
tx = "insert into books(ISBN,书本名,出版社,出版日期,作者,价格,评论,页数,类别) values(";

if (tbxISBN.Text != "")
tx += "'" + tbxISBN.Text + "'" + ",";
else
tx += "''" + ",";

if (tbxBookName.Text != "")
tx += "'" + tbxBookName.Text + "'" + ",";
else
tx += "''" + ",";

if (tbxPublish.Text != "")
tx += "'" + tbxPublish.Text + "'" + ",";
else
tx += "''" + ",";

if (tbxPDate.Text != "")
tx += "#" + tbxPDate.Text + "#" + ",";
else
tx += "" + ",";

if (tbxAuthor.Text != "")
tx += "'" + tbxAuthor.Text + "'" + ",";
else
tx += "''" + ",";

if (tbxPrice.Text != "")
tx += tbxPrice.Text + ",";
else
tx += "0" + ",";

if (tbxComments.Text != "")
tx += "'" + tbxComments.Text + "'" + ",";
else
tx += "''" + ",";

if (tbxPages.Text != "")
tx += tbxPages.Text + ",";
else
tx += "0" + ",";

tx += cbxCategory.SelectedValue.ToString() + ")";

return tx;
}
其中:

if (tbxPDate.Text != "")
tx += "#" + tbxPDate.Text + "#" + ",";

else
tx += "" + ",";

有问题,应该怎样修改?

最佳答案
  • 五星知识达人网友:爱难随人意
  • 2021-04-23 09:57
tbxPDate.Text 应为Date是datetime类型,所以应写成tbxPDate.Text .ToString()
全部回答
  • 1楼网友:旧脸谱
  • 2021-04-23 10:52
需要注意数据类型哈 童鞋...
  • 2楼网友:野慌
  • 2021-04-23 10:20
null试下。
  • 3楼网友:蓝房子
  • 2021-04-23 10:13

数据库中出版日期是什么类型?

tx += "#" + tbxPDate.Text + "#" + ","; 要是数据库为DateTime类型肯定加不进去的.

把数据库设置为varchar(20)

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯