用了Encoding.Default
在窗体的TEXTBOX里显示无乱码··
但是加到数据库里(加进去的时候也用了Encoding.Default格式化了)

在数据库里有点乱码···一些字正常··一些字是?号

。。

解决方案 »

  1.   

    同上utf8,默认好像是根据操作系统来的
      

  2.   

    在读写txt文本文件时最好明确指明编码,用default不是明智的选择,在不同的机器上default是不同的。最好用 UTF8
      

  3.   

    最好用UTF-8编码。
    令:数据库是啥编码的?
      

  4.   

    StreamReader sr=File.OpenText(Server.MapPath(".")+"\myText.txt");
     StringBuilder output=newStringBuilder("a,txt",Encoding.GetEncoding("gb2312"));
     string rl;
     while((rl=sr.ReadLine())!=null)
     {
      output.Append(rl+"");
     }
    Console.WriteLine("这个文本文件的内容为:" +output.ToString());
     sr.Close();
      

  5.   

    存进去后,再取出来试试?
    应该不会丢数据的,再有数据库是nvarchar 还是varchar ?也有一定关系的
      

  6.   


            private void btnYes_Click(object sender, EventArgs e)
            {
                if (txtContent.Text.Trim() != "")
                {
                    string Name = string.Format(txtName.Text, Encoding.UTF8);
                    string Content = string.Format(txtContent.Text, Encoding.UTF8);
                    string sql = string.Format("insert into BookInfo (BookAuthor,BookName,BookContent) values ('{0}','{1}','{2}')", txtAuther.Text, Name,Content);
                    SqlCommand cmd = new SqlCommand(sql, DBHelper.connection);
                    try
                    {
                        DBHelper.connection.Open();
                        int x = cmd.ExecuteNonQuery();
                        if (x > 0)
                        {
                            MessageBox.Show("增加成功", "提示", MessageBoxButtons.OK);
                        }
                        else
                        {
                            MessageBox.Show("增加失败", "提示", MessageBoxButtons.OK);
                        }
                    }
                    catch (Exception ex)
                    {                    MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        DBHelper.connection.Close();
                    }
                }        private void btnAdd_Click(object sender, EventArgs e)
            {
                OpenFileDialog open = new OpenFileDialog();//创建打开文件的对话框
                open.Filter = "txt文本文档|*.txt";// 文件类型筛选
                open.RestoreDirectory = true;            if (DialogResult.OK == open.ShowDialog())
                {
                    //Text.frm Image.FromFile(
                    //txtName.Text = open.Title;
                    string x = Path.GetFileName(open.FileName);
                    string[] y;
                    y = x.Split('.');//把文件名以点分隔两边,放入y数组中
                    this.txtName.Text = "《" + y[0] + "》";
                    path = open.FileName;
        
                    try
                    {
                        FileStream fs = new FileStream(path, FileMode.Open);
                        using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                        {
                            txtContent.Text = sr.ReadToEnd();//读数据
                        }
                        fs.Close();
                    }
                    catch (Exception wrong)
                    {
                        MessageBox.Show(wrong.Message);
                    }
                }
            }数据库里的列BookAuthor和 BookName是nvarchar
     BookContent是ntext
    ...还是无解...一部分字是?号
      

  7.   


    自己写个测试  判断一下textbox里有文字时的编码再用那个编码存数据库看看.你自己多试试.
      

  8.   

    这个应该是和你数据库的字符集有关系,如果数据库字符集是us7ascii的话,你连接数据库如果不是微软公司提供的oledb驱动程序的话就有问题。
    具体如“provide=msdaora;......”