我想SQL插入更新删除的查询 [英] i want sql insert update delete's query

查看:86
本文介绍了我想SQL插入更新删除的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨...我要我的朋友..
我有3场
第一个字段是自动递增
第二个是文本字段
第三个if参考键
这是我的领域详细信息..
如何使用网格视图删除数据,插入数据,更新数据
请帮我,因为我不能做任何事情

hi... i request my friend..
i have 3 field
1st field is auto increment
2nd is text field
3rd if reference key
this is my field details so..
how can i deleting data ,inserting data, updating data using grid view
help me please,, because i can''t do anything with out it

推荐答案

您需要阅读有关SQL Server和DML的信息.拿起一本书阅读.
You need to read about SQL Server and DML. Pick up a book and read.


protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }

    }


private void BindData()
    {
        string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=database_name;Integrated Security=True";
        string query = "SELECT 1st_column,2nd_column,3rd_column FROM Table_Name";
        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();

    }

protected void lnkadd_Click(object sender, EventArgs e)
    {

        String connectionString = @"Data Source=SQLEXPRESS2008;Initial Catalog=database_name;Integrated Security=True";

        SqlConnection thisConnection = new SqlConnection(connectionString);
        try
        {
            thisConnection.Open();
            String thisQuery = "INSERT INTO Table_Name(2nd_column,3rd_column) VALUES ('" + txt2nd_column.Text + "','" + txt3rd_column.Text + "')";

            SqlCommand thisCommand = new SqlCommand(thisQuery, thisConnection);
            thisCommand.ExecuteNonQuery();
            lblmessage.Text = "Record Added Successfully !!!";
            thisConnection.Close();

        }
        catch (SqlException exp)
        {
            lblmessage.Text = exp.Message;
            Console.WriteLine(exp.Message);
   }

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        BindData();
    }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];

        TextBox txt2nd_column = (TextBox)row.FindControl("txt2nd_column");
        TextBox txt3rd_column = (TextBox)row.FindControl("txt3rd_column");

        int a = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string b = txt2nd_column.Text;
        string c = txt3rd_column.Text;

        UpdateProduct(a, b,c);

    }

private void UpdateProduct(int a, string b,string c)
    {
        try
        {
            string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=Database;Integrated Security=True";
            string query = "UPDATE Table_Name SET 2nd_column = @b, 3rd_column = @c WHERE 1st_column = @a";


            SqlConnection con = new SqlConnection(constr);
            SqlCommand com = new SqlCommand(query, con);

            com.Parameters.Add("@a", SqlDbType.Int).Value = 1st_column;
            com.Parameters.Add("@b", SqlDbType.NVarChar).Value = 2nd_column;
            com.Parameters.Add("@c", SqlDbType.NVarChar).Value = 3rd_column;


            con.Open();
            com.ExecuteNonQuery();
            con.Close();


            GridView1.EditIndex = -1;
            BindData();
        }
        catch (Exception ex)
        {
            throw ex;
    }

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
	try
	{
	string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=Database;Integrated Security=True";
	string query = "DELETE FROM Table_Name WHERE 1st_column=@a";

	SqlConnection con = new SqlConnection(constr);
	SqlCommand cmd = new SqlCommand();

	cmd.Parameters.Add("@a", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text)";
	cmd.Connection = con;

	con.Open();
	cmd.ExecuteNonQuery();

	con.Close();
	BindData();
	}
	catch(Exception ex)
	{
	    throw ex;
	}
  }


这篇关于我想SQL插入更新删除的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆