为什么我想要执行添加操作,却添加不成功,系统提示我comm.ExecuteNonQuery有错误
已找到原因
在
string strsql = "insert into tb_goods(产品编号,产品名称,产品价格,生产日期,产品描述) values('"+Label1.Text+"','" + TextBox1.Text + "','"+TextBox3.Text +"','"+TextBox2.Text +"','"+TextBox4.Text+"')";
这行代码里,第一个括号里的“,”用的不是英文的,改为英文的就好了
现在已经好使了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection("server=ZZF-COMPUTER;database=db_ASPNET;integrated security=true");
string cc = "server=ZZF-COMPUTER;database=db_ASPNET;integrated security=true";
NumService num = new NumService();
string cmdtxt = "select * from tb_goods";
string Numstr = num.ProNum(cc, cmdtxt);
this.Label1.Text = Numstr;
SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, new SqlConnection(cc));
DataSet ds = new DataSet();
Da.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection("server=ZZF-COMPUTER;database=db_ASPNET;integrated security=true");
string strsql = "insert into tb_goods(产品编号,产品名称,产品价格,生产日期,产品描述) values('"+Label1.Text+"','" + TextBox1.Text + "','"+TextBox3.Text +"','"+TextBox2.Text +"','"+TextBox4.Text+"')";
SqlCommand comm = new SqlCommand("proc_Insert", sc);
if(sc.State.Equals(ConnectionState.Closed))
{
sc.Open();
}
if(Convert.ToInt32(comm.ExecuteNonQuery())>0)
{
Response.Write("成功");
}
else
{
Response.Write("失败");
}
if (sc.State.Equals(ConnectionState.Open))
sc.Close();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// NumService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class NumService : System.Web.Services.WebService {
public NumService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string ProNum(string ConnectionString, string Cmdtxt)
{
SqlConnection Con = new SqlConnection(ConnectionString);
Con.Open();
SqlDataAdapter Da = new SqlDataAdapter(Cmdtxt, Con);
DataSet ds = new DataSet();
Da.Fill(ds);
int Num = ds.Tables[0].Rows.Count;
return "NO" + DateTime.Now.ToString("yyyyMMdd") + "BH" + Convert.ToString(Num + 1);
}
}
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data;using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { SqlConnection sc = new SqlConnection("server=ZZF-COMPUTER;database=db_ASPNET;integrated security=true"); string cc = "server=ZZF-COMPUTER;database=db_ASPNET;integrated security=true"; NumService num = new NumService(); string cmdtxt = "select * from tb_goods"; string Numstr = num.ProNum(cc, cmdtxt); this.Label1.Text = Numstr; SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, new SqlConnection(cc)); DataSet ds = new DataSet(); Da.Fill(ds); this.GridView1.DataSource = ds; this.GridView1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection sc = new SqlConnection("server=ZZF-COMPUTER;database=db_ASPNET;integrated security=true"); string strsql = "insert into tb_goods(产品名称,产品价格,生产日期,产品描述) values('" + TextBox1.Text + "','" + TextBox3.Text + "','"+TextBox2.Text+"','"+TextBox4.Text+"')"; SqlCommand comm = new SqlCommand("proc_Insert", sc); if(sc.State.Equals(ConnectionState.Closed)) { sc.Open(); } if(Convert.ToInt32(comm.ExecuteNonQuery())>0) { Response.Write("成功"); } else { Response.Write("失败"); } if (sc.State.Equals(ConnectionState.Open)) sc.Close(); }}