1. 程式人生 > >利用Web服務生成產品編號 執行新增操作

利用Web服務生成產品編號 執行新增操作

為什麼我想要執行新增操作,卻新增不成功,系統提示我comm.ExecuteNonQuery有錯誤

下面是我的程式碼

哪位大佬可以教教我,到底哪裡錯了

非常感謝

 

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();

        

    }

}

 

 

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();            }}