求助,程序问题?
using System;using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _13_03 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible=false;
Label2.Visible = false;
}
protected void button1_Click(object sender, EventArgs e)
{
//声明三个字符串变量接收用户输入的值
string strtitle = TextBox1.Text.ToString();
string strwiter = TextBox2.Text.ToString();
string strcontent = TextBox3.Text.ToString();
SqlConnection con = new SqlConnection(@"Data Source=localhost\SQLEXPRESS; database=test;uid=sa;pwd=;");
con.Open();
//使用用户输入的值来构造新增数据的SQL语句
string strInsert =string.Format("insert into article(title,writer,content) values ('{0}','{1}','{2}')", strtitle, strwiter, strcontent);
SqlCommand cmd = new SqlCommand(strInsert, con);
//ExecuteNoQuery()方法来执行没有返回结果的命令
cmd.ExecuteNonQuery();
con.Close();
Label1.Text = "所执行语句为:<br>";
Label2.Text = "状态,执行成功!!!";
Label1.Visible = true;
Label2.Visible = true;
}
}
错误提示:不允许从数据类型 varchar 到 varbinary 的隐式转换。请使用 CONVERT 函数来运行此查询。
然后改成以下的形式,还是不行,怎么回事,该怎么解决?
string strtitle = Convert.ToString(TextBox1.Text.ToString());
string strwiter =Convert .ToString (TextBox2.Text.ToString());
string strcontent = Convert.ToString (TextBox3.Text.ToString());
string strInsert = Convert.ToString (string.Format("insert into article(title,writer,content) values ('{0}','{1}','{2}')", strtitle, strwiter, strcontent));