⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 default.aspx.cs

📁 这是asp.net^和Visual C++Sharp编写的串并口通讯的书籍 源代码
💻 CS
字号:
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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();

        }
    }
    public void BindData()
    {
        SqlConnection myconn = new SqlConnection("server=(local);Database=mrdb;Uid=sa;Pwd=");
        myconn.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from tb_Book order by BookID desc", myconn);
        DataSet ds = new DataSet();
        da.Fill(ds, "tb_BookInfo");
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] {"BookID"};
        GridView1.DataBind();
        myconn.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection myconn = new SqlConnection("server=(local);Database=mrdb;Uid=sa;Pwd=");
        //打开链接
        myconn.Open();
        //通过SqlConnection的BeginTransaction方法创建名为st的对象Transaction
        SqlTransaction st = myconn.BeginTransaction();
        string str = "select count(*) from tb_BookInfo where BookName='" + TextBox1.Text.ToString() + "'";
        SqlCommand com = new SqlCommand(str, myconn);
        //将com与事务建立联系
        com.Transaction = st;
        int intcont = Convert.ToInt32(com.ExecuteScalar());
        if (intcont > 0)
        {
            Response.Write("<script language=javascript>alert('对不起!不允许填写相同记录!')</script>");
        }
        else
        {
            try
            {
                SqlCommand mycom = new SqlCommand("probookinfo", myconn);
                //将mycom与事务建立联系
                mycom.Transaction = st;
                //调用存储过程
                mycom.CommandType = CommandType.StoredProcedure;
                //添加参数
                SqlParameter[] prams ={

                           new SqlParameter("@BookName", SqlDbType.VarChar, 50),
                           new SqlParameter("@BookIntroduce", SqlDbType.VarChar, 50),
                           new SqlParameter("@BookPrice", SqlDbType.Money, 8),
                           new SqlParameter("@BookIsNew", SqlDbType.Char, 10),
                                                      
                 };
                //给参数赋值
                prams[0].Value = TextBox1.Text;
                prams[1].Value = TextBox2.Text;
                prams[2].Value = Convert.ToDecimal(TextBox3.Text);
                prams[3].Value = DropDownList1.SelectedValue.ToString();
                foreach (SqlParameter parameter in prams)
                {
                    mycom.Parameters.Add(parameter);
                }
                //执行存储过程
                mycom.ExecuteNonQuery();
                //提交事务
                st.Commit();
                BindData();
                Response.Write("<script language=javascript>alert('添加成功!')</script>");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
                //回滚事务
                st.Rollback();
            }
            finally
            {
                myconn.Close();
            }
        }
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        this.TextBox1.Text = "";
        this.TextBox2.Text = "";
        this.TextBox3.Text = "";
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from tb_Book where BookID='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
        SqlConnection myconn = new SqlConnection("server=(local);Database=mrdb;Uid=sa;Pwd=");
        SqlCommand sqlcom = new SqlCommand(sqlstr,myconn);
        myconn.Open();
        sqlcom.ExecuteNonQuery();
        myconn.Close();
        BindData();

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -