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!