1. 程式人生 > >sqlserver中變長字串與定長字串

sqlserver中變長字串與定長字串

最近做一個工廠的自動化專案使用到sqlserver,查詢其中某個值的時候返回的是空值,研究了一下發現該值的型別是nchar,定長unicode編碼,網上查了下nchar和nvarchar的區別,nvarchar型別儲存時會按字長實際長度兩倍儲存,進一步通過程式驗證發現nchar(length)儲存時按開闢空間的長度進行儲存的,餘位會補空,讀取到C#中該值長度是length而不是實際長度。以下是測試程式碼
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "server=;database=;user=;password=";
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                try
                {
                    con.Open();
                }
                catch (SqlException e)
                {
                    throw e;
                }
                if (con.State == ConnectionState.Open)
                {
                    string insertCmd = $"insert into InvoiceCardState(ChrBoxUp,ChrLot) values('KS','KS')";
                    int rows=ExecuteSql(insertCmd, con);

                    DataTable dt = new DataTable();
                    if(rows>0)
                    {
                        string queryCmd = $"select * from Test where ChrBoxUp='KS'";
                        QuerySql(queryCmd, con, dt);
                        if(dt.Rows.Count>0)
                        {
                            string lot = dt.Rows[0]["ChrLot"].ToString();
                            string pb = dt.Rows[0]["ChrBoxUp"].ToString();
                            int lotl = lot.Length;
                            int pbl = pb.Length;
                            Console.WriteLine("lot length:" + lotl.ToString());
                            Console.WriteLine("pb length:" + pbl.ToString());
                        }
                    }
                }

            }
        }

        static int ExecuteSql(string sqlcmd, SqlConnection con)
        {
            int rows = 0;
            SqlCommand cmd = new SqlCommand(sqlcmd, con);
            try
            {
                rows = cmd.ExecuteNonQuery();
            }
            catch (SqlException e)
            {
                throw e;
            }
            return rows;
        }

        static int QuerySql(string sqlcmd, SqlConnection con, DataTable dt)
        {
            int errorCode = 0;
            SqlCommand cmd = new SqlCommand(sqlcmd, con);
            using (SqlDataAdapter adpter = new SqlDataAdapter(cmd))
            {
                try
                {
                    adpter.Fill(dt);
                }
                catch (SqlException e)
                {
                    errorCode = e.ErrorCode;
                }
            }            
            return errorCode;
        }
    }
}
其中Test表中ChrBoxUp為nvarchar(12),ChrLot型別為nchar(12),均存入欄位`KS`,然後讀取,結果ChrLot長度度為12,ChrBoxUp長度為2,加斷點監視`string lot`的值`KS`右端存在一部分空格,並且如果將查詢條件改為`where ChrLot='KS'`,查詢結果為空。