LocalDB自然好,比起Sql Server轻量,但是在我的电脑上(windows10 + vs2015)使用ado.net执行sql插入中文就乱码。
这个问题困扰我好久了,网上找了一下,终于找到一个简单的方法。见csdn论坛:http://bbs.csdn.net/topics/390725751?page=1
就是将插入的中文字符串'cn-string'前面加N。
例如这样:
insert into table1(T1) values(N'C')
因为我在项目中遇到这个问题,我的这一块代码如下,我要将性别插入到UserTable表里的Sex列中,注意我的update语句:
string SexCk = RadioButton1.Checked ? "男":"女";
string AgeText = AgeTb.Text;
int Age = -1;
try
{
Age = int.Parse(AgeText);
}
catch
{
Response.Write("输入整数");
}
string connStr = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True";
SqlConnection conn = new SqlConnection(connStr);
try
{
conn.Open();
string sqlselect;
if (Session["CurrentUser"] != null)
{
sqlselect = "update UserTable set Sex = N'" + SexCk + "', Age = '" + Age + "' where UId='" + Session["CurrentUser"] + "'";
SqlCommand cmd = new SqlCommand(sqlselect, conn);
cmd.ExecuteNonQuery();
Response.Redirect("UserInfo.aspx");
}
}
catch (Exception ee)
{
Response.Write(ee.ToString());
}
finally
{
conn.Close();
}