需求如下:
我在往SQL数据库中写内容时,输入了空格,在数据表里的数据,也的确保存进去了空格。但是我读取数据表的内容,在另外一个页面显示的时候,空格是没有被读出来的,只读出了文字。我希望在读出来的数据能把空格也读出来,这样就保证我写信息的界面和我读信息的界面内容和效果完全一样。因为这个页面是部门之间的联系函,页面的格式很重要,请高手指点一下!

解决方案 »

  1.   

    绑定到TextBox和Lable两种控件上,字段类型都是varchar!
      

  2.   

    用nvarchar试试。我用nchar和nvarchar是没有问题的
    再次测试绑定到textbox也没问题
      

  3.   

    我试了一下,还是没有空格啊,我绑定数据的代码是:
             SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
            string strsql = "select * from ims_conncode where conn_id='" + conn_id + "'";
            SqlDataAdapter sda = new SqlDataAdapter(strsql, con);
            DataSet ds = new DataSet();
            sda.Fill(ds);
            if(ds.Tables[0].Rows.Count>0)
            {
                labnum.Text = ds.Tables[0].Rows[0][1].ToString();
                labinf.Text = ds.Tables[0].Rows[0][2].ToString();
                labtodept.Text = ds.Tables[0].Rows[0][3].ToString();
                labcontent.Text = ds.Tables[0].Rows[0][4].ToString().Trim();
                labfromdept.Text = ds.Tables[0].Rows[0][5].ToString();
                labdate.Text = ds.Tables[0].Rows[0][6].ToString();
            }
    我把数据类型该成nvarchar但是效果还是没空格!其中labcontent是绑定有空格数据的!
      

  4.   

       labcontent.Text = ds.Tables[0].Rows[0][4].ToString().Trim(); 
    我靠你太有才你用Trim()把空格都清完了。还怎么显示空格
      

  5.   

    不是,那段Trim()本来是没有的,我后来加上去的,我再试一下用nvarchar可行不可行!
      

  6.   

    事实证明,还是不行啊,我确定数据库中的数据是有空格的,那段trim()也去掉了,但是显示的还没空格!
      

  7.   

     SqlConnection conn = new SqlConnection("Data Source=KING;Initial Catalog=Demo;Integrated Security=True");
                SqlCommand cmd = new SqlCommand("select name from student where id=2",conn);
                conn.Open();
                string t = (string)cmd.ExecuteScalar();
                textBox1.Text = t;
                label1.Text ="d"+t;这是我测试的代码。。返回的t是有空格的。一点问题也没有。。
      

  8.   

    这个经过我的测试,用textBox控件可以显示空格,用label控件就显示不出来!