Home > Software engineering >  Database connection of DBHelper class, the code inserted in the database after Chinese display a que
Database connection of DBHelper class, the code inserted in the database after Chinese display a que

Time:11-19

Learning Windows programming this is a database connection DBHelper class, code inserted in the database after Chinese display a question mark, know to add charset=utf8, but,,, I don't know what  very simple question, please teach

The namespace KDSystem
{
The class DBHelper
{
Public static SqlConnection GetConnection ()
{

//the connection string
String constr=@ "Data Source=(LocalDB) \ MSSQLLocalDB; AttachDbFilename=C: \ Users \ Desktop \ Windows \ \ DE KDSystem \ KDSystem \ KDSystem \ KDS MDF. Integrated Security=True";
SqlConnection con=new SqlConnection (constr);
Return con;

}
}
}

CodePudding user response:

//insert the data code, the running error: later the SQL user changed to [user], can insert, but stil
SqlConnection con=DBHelper. GetConnection ();
Con. The Open ();
If (con) State==ConnectionState) Open)
{

String SQL="insert into [user] values ('" + textBox1. Text + "', '" + textBox2. Text +") ";
//SQL=string. The string Format (" insert into user values (' {0} ', '{1}') ", the textBox1. Text, textBox2. The Text).
SqlCommand CMD=new SqlCommand (SQL, con);
If (CMD) ExecuteNonQuery () & gt; 0)
{
MessageBox. Show (" registered success!" );
}
The else
MessageBox. Show (" registration failed!" );
}