MSSQLSERVER import into the database Insert text garbled problem solving method

Source: Internet
Author: User
Tags character set mssqlserver

MSSQLServer import into the database tutorial insert text garbled problem resolution, first look at my import code, there should be no problem.

private void Btninsert_click (object sender, EventArgs e)
{
if (odimport). ShowDialog () = = DialogResult.OK)//prepare to receive the file;
{
using (FileStream FileStream = File.openread (odimport. FileName))
{
using (StreamReader StreamReader = new StreamReader (FileStream))
{
//create connection string and establish channel;
String connstr = configurationmanager.connectionstrings["Constr"]. ConnectionString;
using (SqlConnection conn = new SqlConnection (connstr))
{
Conn. Open (); The
using (SqlCommand cmd = conn. CreateCommand ())
{
cmd. CommandText = "INSERT into PhoneNumber (Phone,localid,localaddress,phonestyle) VALUES (@Phone, @LocalID, @LocalAddress, @Phonestyle) ";
String line = null;
while (line = StreamReader. ReadLine ())!= null)
{
//convert each row of data to an array and split the string with split, get field information,
string[] STRs = line. Split (")";
String Phone = Strs[0];
int localid = Convert.ToInt32 (strs[1]);
String localaddress = strs[2];
String phonestyle = strs[3];

Cmd. Parameters.clear ();
Cmd. Parameters.Add (New SqlParameter ("Phone", phone));
Cmd. Parameters.Add (New SqlParameter ("Localid", Localid));
Cmd. Parameters.Add (New SqlParameter ("LocalAddress", localaddress));
Cmd. Parameters.Add (New SqlParameter ("Phonestyle", Phonestyle));
Cmd. ExecuteNonQuery ();
}
}

}
}
}
MessageBox.Show ("Import succeeded");
}

Debugging is normal, but when I open the datasheet to view the imported data, the pain occurs: The following map attribution and the card product information all become unknown characters

At first I thought it was a field property set error, check the field properties is wrong, but a check no big problem:

Then the internet searched a lot of information about the problem of inserting text garbled in the database; Do not look good, the more see more dizzy, unexpectedly there is a man said this is MSSQLSERVER data protection, what encryption field, by thinking of Microsoft will not so will not be so BT, and some say is the database character set problem, I tried to come to the conclusion that it was all nonsense!

Change the database code is not, the last sudden thought of the change to be imported text document encoding finally the text document from ANSI to Unicode, the last import; by success.

Eggs hurt Ah, calm!

After an afternoon of tossing and drawing a conclusion: programming is really a very let the human egg pain! Endure the pain, you become calm!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.