关于数据库错误

hsc1992 2017-07-26 07:11:31


时间我在数据库定义为DateTime型,其余全为nchar(10)类型


//数据库连接字符串
string str = "Data Source=lenovo-pc;Initial CataLog =猪舍环境参数0725;Integrated Security=True";

using (SqlConnection con = new SqlConnection(str))
{
//定义sql语句,此处注意获得的values的值写法
string sql = string.Format("insert into 猪舍环境0725 (时间,温度,湿度,光照,二氧化碳,氨气) values ('" + sj + "','" + TemptextBox.Text + "','" + HumtextBox.Text + "','" + IlltextBox.Text + "','" + CO2textBox.Text + "','" + NH3textBox.Text + "')");
using (SqlCommand cmd = new SqlCommand(sql, con))
{
//打开数据库
con.Open();
cmd.ExecuteNonQuery();
}
}

错误显示sql语句中' '有问题,(sj为datetime类型,其余为字符串)
...全文
190 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
吉普赛的歌 2017-07-27
  • 打赏
  • 举报
回复
改了一下, 你原来确实有中文逗号的问题
            string sql = "insert into 猪舍环境0725 (时间,温度,湿度,光照,二氧化碳,氨气) values (@p0,@p1,@p2,@p3,@p4,@p5)";
            try
            {
                using (SqlConnection con = new SqlConnection(str))
                {
                    con.Open();
                    
                    //对于输入的值,不要拼SQL, 而应该用参数化来代替
                    //一是会带来 sql 注入, 不安全;
                    //二是容易出错;
                    //三是效率不高, 执行计划没办法重用
                    SqlParameter[] spArr = new SqlParameter[]{
                    new SqlParameter("@p0", sj),
                    new SqlParameter("@p1", TemptextBox.Text),
                    new SqlParameter("@p2", HumtextBox.Text),
                    new SqlParameter("@p3", IlltextBox.Text),
                    new SqlParameter("@p4", CO2textBox.Text),
                    new SqlParameter("@p5", NH3textBox.Text)
                };

                    SqlCommand cmd = new SqlCommand(sql, con); //无须using, con 在 using结束时会自动关闭, 相关对象也会自动关闭
                    cmd.Parameters.AddRange(spArr);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                string errMsg = string.Format("sql: {0} \r\n 错误原因:{1}", sql, ex.Message);
                MessageBox.Show(errMsg);
            }
吉普赛的歌 2017-07-27
  • 打赏
  • 举报
回复
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                string sql = "insert into 猪舍环境0725 (时间,温度,湿度,光照,二氧化碳,氨气) values (@p0,@p1,@p2,@p3,@p4,@p5)";
                //对于输入的值,不要拼SQL, 而应该用参数化来代替
                //一是会带来 sql 注入, 不安全;
                //二是容易出错;
                //三是效率不高, 执行计划没办法重用
                SqlParameter[] spArr = new SqlParameter[]{
                    new SqlParameter("@p0", sj),
                    new SqlParameter("@p1", TemptextBox.Text),
                    new SqlParameter("@p2", HumtextBox.Text),
                    new SqlParameter("@p3", IlltextBox.Text),
                    new SqlParameter("@p4", CO2textBox.Text),
                    new SqlParameter("@p5", NH3textBox.Text)
                };
                
                SqlCommand cmd = new SqlCommand(sql, con); //无须using, con 在 using结束时会自动关闭, 相关对象也会自动关闭
                cmd.Parameters.AddRange(spArr);
                cmd.ExecuteNonQuery();
            }
OwenZeng_DBA 2017-07-27
  • 打赏
  • 举报
回复
引用 2 楼 hsc1992的回复:


类型不匹配?
看这里的报错是把逗号打错成,中文的逗号了。你把逗号都检查下
吉普赛的歌 2017-07-27
  • 打赏
  • 举报
回复
引用 16 楼 hsc1992 的回复:
确实是,逗号问题,嗯嗯,确实是,尽量不要用拼接字符串,你这个方法可以的
一分也无, 浪费时间了
hsc1992 2017-07-27
  • 打赏
  • 举报
回复
引用 15 楼 yenange 的回复:
改了一下, 你原来确实有中文逗号的问题
            string sql = "insert into 猪舍环境0725 (时间,温度,湿度,光照,二氧化碳,氨气) values (@p0,@p1,@p2,@p3,@p4,@p5)";
            try
            {
                using (SqlConnection con = new SqlConnection(str))
                {
                    con.Open();
                    
                    //对于输入的值,不要拼SQL, 而应该用参数化来代替
                    //一是会带来 sql 注入, 不安全;
                    //二是容易出错;
                    //三是效率不高, 执行计划没办法重用
                    SqlParameter[] spArr = new SqlParameter[]{
                    new SqlParameter("@p0", sj),
                    new SqlParameter("@p1", TemptextBox.Text),
                    new SqlParameter("@p2", HumtextBox.Text),
                    new SqlParameter("@p3", IlltextBox.Text),
                    new SqlParameter("@p4", CO2textBox.Text),
                    new SqlParameter("@p5", NH3textBox.Text)
                };

                    SqlCommand cmd = new SqlCommand(sql, con); //无须using, con 在 using结束时会自动关闭, 相关对象也会自动关闭
                    cmd.Parameters.AddRange(spArr);
                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                string errMsg = string.Format("sql: {0} \r\n 错误原因:{1}", sql, ex.Message);
                MessageBox.Show(errMsg);
            }
确实是,逗号问题,嗯嗯,确实是,尽量不要用拼接字符串,你这个方法可以的
二月十六 2017-07-26
  • 打赏
  • 举报
回复
用sql server profiler跟踪一下,看看程序执行的

引用 11 楼 hsc1992 的回复:
引用 10 楼 sinat_28984567 的回复:
用sql server profiler跟踪一下,看看程序执行的语句是不是这句
不懂。没用过。。。

很简单也很好用,教程也很多,搜索一下学一下
http://www.cnblogs.com/kissdodog/p/3398523.html
hsc1992 2017-07-26
  • 打赏
  • 举报
回复
引用 10 楼 sinat_28984567 的回复:
用sql server profiler跟踪一下,看看程序执行的语句是不是这句
不懂。没用过。。。
二月十六 2017-07-26
  • 打赏
  • 举报
回复
用sql server profiler跟踪一下,看看程序执行的语句是不是这句
hsc1992 2017-07-26
  • 打赏
  • 举报
回复
引用 8 楼 wmxcn2000 的回复:
看起来是没有问题的

把拼接完的 sql 打印一下,贴到 ssms 中,执行一下;


这就蛋疼啦
卖水果的net 2017-07-26
  • 打赏
  • 举报
回复
看起来是没有问题的 把拼接完的 sql 打印一下,贴到 ssms 中,执行一下;
hsc1992 2017-07-26
  • 打赏
  • 举报
回复
引用 6 楼 hsc1992 的回复:
[quote=引用 5 楼 wmxcn2000 的回复:] MessageBox.Show(sql); // 这里打印一下 using (SqlCommand cmd = new SqlCommand(sql, con))
[/quote] 是不是时间格式原因?
hsc1992 2017-07-26
  • 打赏
  • 举报
回复
引用 5 楼 wmxcn2000 的回复:
MessageBox.Show(sql); // 这里打印一下
using (SqlCommand cmd = new SqlCommand(sql, con))
卖水果的net 2017-07-26
  • 打赏
  • 举报
回复
MessageBox.Show(sql); // 这里打印一下 using (SqlCommand cmd = new SqlCommand(sql, con))
hsc1992 2017-07-26
  • 打赏
  • 举报
回复
引用 3 楼 wmxcn2000 的回复:
把拼接完以后的,粘贴过去。
问题是sj报错,不知道怎么改
卖水果的net 2017-07-26
  • 打赏
  • 举报
回复
把拼接完以后的,粘贴过去。
hsc1992 2017-07-26
  • 打赏
  • 举报
回复


类型不匹配?
卖水果的net 2017-07-26
  • 打赏
  • 举报
回复
把拼接完的 sql 打印一下,贴到 ssms 中,执行一下;

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧