注册 登录
编程论坛 ASP.NET技术论坛

求助,程序问题?

zore0 发布于 2008-10-14 15:23, 730 次点击
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));
3 回复
#2
kevintang2008-10-14 16:02
错误提示:不允许从数据类型 varchar 到 varbinary 的隐式转换。请使用 CONVERT 函数来运行此查询。 是不是数据库中的数据类型有问题啊?
  自己调试下看看啊?
#3
bygg2008-10-15 12:37
数据库设计有问题.将里面的varbinary 改成 varchar
#4
zore02008-10-15 17:11
谢谢,现在可以了
1