MS-SQL中Insert操作插入的中文變成問號”???”的解決辦法

來源:互聯網
上載者:User

      編寫的新聞管理系統添加新聞以後標題和內容都變成了"???",插入語句如下:

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;

修改完成以後再插入 中文就不會再顯示問號了。

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.