strSql = "INSERT INTO Articles ([Article_Subject] ,[Article_Content] ,[Author_ID] ,[Post_Time]) VALUES ('测试','测试','FrancisLiu','"+DateTime.Today.ToShortDateString()+"')";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = strSql;
cmd.ExecuteNonQuery();
strSql.Append("insert into [Articles](");
strSql.Append("[article_Subject],[article_Content],[author_id],[post_time])");
strSql.Append(" values (");
strSql.Append("@article_Subject,@article_Content,@author_id,@make_time,@sReply_count)");
SqlParameter[] parameters = {
new SqlParameter("@article_Subject", SqlDbType.NChar,40),
new SqlParameter("@article_Content", SqlDbType.NText),
new SqlParameter("@author_id", SqlDbType.NChar,10),
new SqlParameter("@make_time", SqlDbType.NChar,25)
};
parameters[0].Value = model.article_Subject;
parameters[1].Value = model.article_Content;
parameters[2].Value = model.author_id;
parameters[3].Value = model.make_time; |
这个问题很怪呀,直接用strSql 在数据库的查询分析器中执行会怎么样呢? 两个字段都是乱码吗? 你可以调试一下程序,在cmd.ExecuteNonQuery();这句上设置断点然后分别查看cmd.CommandText和strSql的值是什么,看是否有乱码存在,也就是在把命令发送到数据库前检查写入的数据是否有乱码。 如果没有,那么启动sql profile(事件探查器)看实际执行的语句是什么,是不是在从程序到数据库这个过程中出了问题。 怀疑你的程序编码方式和数据库编码不一致才造成这个问题 试试在字符串前加个N行不行.. strSql = "INSERT INTO Articles ([Article_Subject] ,[Article_Content] ,[Author_ID] ,[Post_Time]) VALUES (N'测试',N'测试','FrancisLiu','"+DateTime.Today.ToShortDateString()+"')"; 是单引号的问题吧,试试 strSql = @"INSERT INTO Articles ([Article_Subject] ,[Article_Content] ,[Author_ID] ,[Post_Time]) VALUES ('测试','测试','FrancisLiu','" +…… 或者把 ' 变成 \' |