gezhu1212
2021-04-27 15:25
采纳率: 88.9%
浏览 29

C#数据未插入数据库,我这样写有什么问题吗?

                    SqlConnection cn = new SqlConnection("server=" + ComName + ";pwd=;uid=sa;database=数据");
                    SqlCommand cmd = cn.CreateCommand();
                    try
                    {
                        cmd.CommandText = "insert 数据(时间,设备编号,数据源) values("+this.date.Text + this.time.Text+"," + devnumber1[k] + "," + a1 + ")";
                        cmd.ExecuteNonQuery();
                    }
                    catch
                    {

                    }
                    finally
                    {
                        cn.Close();
                    }

调试时的cmd.commamdtext=insert 数据(时间,设备编号,数据源) values(2021年04月27日15:20:00,30 33 32 33 38 38 33 35 34 30 37 32 34 35 35 39 35 38 31 34 ,07 10 00 00 00 4B 96 80 00 00 00 57 6A 85 62 75 AA 0A 96 0B 98 0C 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 02 00 03 00 04 00 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 77 D9 )

观察数据库里并没有数据,这是为什么?是sql语法有什么问题吗?程序有什么错误吗?还是数据库有什么问题?

这是我建的数据库

 

  • 好问题 提建议
  • 收藏

1条回答 默认 最新

  • 已采纳

    sql肯定有问题了,字符串和时间要用单引号扩起。而且日期格式不要加中文年月日,应该这种格式2021-04-27 15:20:00

     

    cmd.CommandText = "insert 数据(时间,设备编号,数据源) values('" + this.date.Text +" "+ this.time.Text + "','" + devnumber1[k] + "','" + a1 + "')";
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题