如何使用 C# 在 asp.net 中创建 class

How to create class in asp.net with c#

我在创建时遇到问题 class

我有 1 个 class 叫 "lib" :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for lib
/// </summary>
public class lib
{
    public static SqlConnection con;
    public static void constr()
    {
        lib.con = new SqlConnection();
        lib.con.ConnectionString = "Data Source=(local);Initial Catalog=FroumDB;Persist Security Info=True;User ID=sa;Password=123";
    }

    //insert
    public static SqlCommand cmd;
    public static void insert(string TableName ,string FieldNames, string values)
    {
        lib.constr();
        lib.cmd = new SqlCommand("insert into " + TableName + " (" + FieldNames + ") values(" + values + ")", lib.con);
        lib.cmd.CommandType = CommandType.Text;

        lib.con.Open();
        lib.cmd.ExecuteNonQuery();
        lib.con.Close();
    }
}

在我使用的网络表单中:

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 ForumPost : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {



        string t = "TPost";
        string f = "PostTitle,PostQuestion";
        string v = "@pt,@pq";

        lib.insert(t, f, v);
        lib.cmd.Parameters.AddWithValue("@pt", this.tbxTitle.Text);
        lib.cmd.Parameters.AddWithValue("@pq", this.tbxText.Text);


        lblshow.Text = "submited !!!";


    }
}

但是当我玩调试时给我这个错误:

Must declare the scalar variable "@pt".

来自此代码 "lib.cmd.ExecuteNonQuery()" 浏览器中出现此错误:

Must declare the scalar variable "@pt".

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@pt".

Source Error:

Line 27: Line 28: lib.con.Open(); Line 29:
lib.cmd.ExecuteNonQuery(); Line 30: lib.con.Close(); Line 31: }

我该如何解决这个错误?

试试这个:

var db = Database.open(/*your database*/);
string sqlcommand = "insert into TPost (PostTitle,PostQuestion,PostAnserw) values(@0,@1,@2)";
db.Execute(sqlcommand,param0,param1,param2);

使用@0、@1 也有一些安全原因,例如防止 sql 注入。

我找到了答案:

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 ForumPost : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {



        string t = "TPost";
        string f = "PostTitle,PostQuestion";
        string v = "@pt,@pq";

        lib.insert(t, f, v);
        lib.cmd.Parameters.AddWithValue("@pt", this.tbxTitle.Text);
        lib.cmd.Parameters.AddWithValue("@pq", this.tbxText.Text);
        lib.con.Open();
        lib.cmd.ExecuteNonQuery();
        lib.con.Close();

        lblshow.Text = "submited !!!";


    }
}