zoukankan      html  css  js  c++  java
  • MSSQL中Insert操作插入的中文变成问号"???"的解决办法

          编写的新闻管理系统添加新闻以后标题和内容都变成了"???",插入语句如下:

    insert into news_info(info_title,info_content) values('标题','内容')

          在网上查了一下,这是因为我的标题和内容的数据类型分别为nvarchar和ntext,而我的数据库排序规则是"SQL_Latin1_General_CP1_CI_AS",只要将其改为"Chinese_PRC_CI_AS"就可以了。可以使用Sql server management studio改,也可以使用sql语句改。sql语句如下:

    alter  database  databaseName  collate Chinese_PRC_CI_AS

          但是我在该数据库排序规则的时候出现错误,错误信息:无法用排它锁锁定该数据库,alter database 操作失败。

          后来发现修改插入语句可以显示中文,就是在字符前面加上字母"N",插入语句如下:

    insert into news_info(info_title,info_content) values(N'标题',N'内容')

          在业务逻辑层也需要进行一些修改,原先业务逻辑层的插入操作如下

      public static bool AddNews(string title, string content)//添加新闻
            {
                String strsql = "Insert into news_info(info_title,info_content,info_addtime,info_isshow,info_chinese) Values(@info_title,@info_content,@info_addtime,@info_isshow,@info_chinese)";
                SqlParameter[] paras = new SqlParameter[5];
                paras[0] = new SqlParameter("@info_title", SqlDbType.VarChar);
                paras[0].Value = title;
                paras[1] = new SqlParameter("@info_content", SqlDbType.VarChar);
                paras[1].Value = content;
                paras[2] = new SqlParameter("@info_addtime", SqlDbType.DateTime);
                paras[2].Value = System.DateTime.Now;
                paras[3] = new SqlParameter("@info_isshow", SqlDbType.Int);
                paras[3].Value = 0;
                paras[4] = new SqlParameter("@info_chinese", SqlDbType.Int);
                paras[4].Value = 1;
                if (NewsDB.Getcmd(strsql, paras))
                {
                    return true;
                }
                return false;
            }

    将其中的SqlDbType.VarChar改为SqlDbType.NVarChar,修改代码如下:

               paras[0] = new SqlParameter("@info_title", SqlDbType.NVarChar);
                paras[0].Value = title;
                paras[1] = new SqlParameter("@info_content", SqlDbType.NVarChar);
                paras[1].Value = content;

    修改完成以后再插入 中文就不会再显示问号了。

    作者:xwdreamer
    欢迎任何形式的转载,但请务必注明出处。
    分享到:
  • 相关阅读:
    小峰视频十三:二维数组
    小峰视频十二:java数组
    小峰视频十一:循环结构的跳出
    小峰视频十:循环while、for
    小峰视频九:选择语句if、switch
    小峰视频八:逻辑运算符、关系运算符、三门运算符
    小峰视频七:数据类型转换、数据运算
    eggjs+vue实现下载图片 js下载网络图片
    报错/Warning: You should not use <Route component> and <Route children> in the same route; <Route component> will be ignored
    React/事件系统
  • 原文地址:https://www.cnblogs.com/xwdreamer/p/2297141.html
Copyright © 2011-2022 走看看