使用多行文本框值检查数据库 [英] Check database with multiline textbox values

查看:74
本文介绍了使用多行文本框值检查数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends,

i我遇到了一个问题,我有两个表客户和公司。

公司表有列ID和公司

客户端有三列client_id,client_name和company_id



页面i有两个文本框,一个说公司,另一个是客户端,文本框,它取客户端的值是多行文本框(这是需要的)。



一切都完成,插入工作正常,删除工作正常但更新时我有问题如何删除那些客户端是存储在表格客户端但更新后它们不应该在那里。







Hello Friends,
i am stuck with a question where i have two tables client and company.
company table has column id and company
client has three column client_id , client_name and company_id

on page i have two text box one says company and other is client , textbox which takes values for client is multiline textbox (which is a need here).

everything is done, insert is working fine, delete is working fine but while updating i have question how to delete those client which are stored in the table client but after updating it they should not be there.



public void isclientpresent(int selectecustomer, string clientname)
 {
     SqlConnection con = new SqlConnection(@"Data Source=AASHISH\SQLEXPRESS;Initial Catalog=customerdb;Integrated Security=True");
     con.Open();
     SqlCommand cmd = new SqlCommand("Select * from client where company_id='"+selectecustomer+"'and client_name='"+clientname+"'", con);
     SqlDataAdapter sqlda = new SqlDataAdapter(cmd);
     DataSet sqlds = new DataSet();
     sqlda.Fill(sqlds);
     con.Close();

     if (sqlds.Tables[0].Rows.Count > 0)
     {
         //
     }
     else
     {
         insertclient(selectecustomer, clientname);
     }

 }



protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
     int selectedcustomer = Convert.ToInt16(GridView1.Rows[e.RowIndex].Cells[3].Text);

     string[] allLines = txtclients.Text.Split('\n');
     foreach (string text in allLines)
     {
         isclientpresent(selectedcustomer, text);
     }

 }

 public void insertclient(int selectedcompany, string selectedclient)
 {
     SqlConnection con = new SqlConnection(@"Data Source=AASHISH\SQLEXPRESS;Initial Catalog=customerdb;Integrated Security=True");
     con.Open();
     SqlCommand cmd = new SqlCommand("Insert into client (client_name,company_id) VALUES('" + selectedclient+ "',"+selectedcompany+")", con);
     cmd.ExecuteNonQuery();
     con.Close();

 }





我的尝试:



当我选择更新GridView1_RowUpdating被拍摄并逐个进入数据库以检查它们是否存在。如果没有,那么他们调用插入查询。



但是如果有数据库但是没有从数组发送来检查。



任何人都可以帮我这个



What I have tried:

When i select update GridView1_RowUpdating is shooted and one by one make to database to check if they exist there or not . if not then they call insert query.

but what if value is there is database but not sent from array to check .

Can anyone help me on this

推荐答案

Quote:

删除存储在表客户端中的那些客户端,但在更新之后它们不应该在那里。

delete those client which are stored in the table client but after updating it they should not be there.





你只需要重新绑定你的GridView反映你所做的改变。因此,当您更新或删除数据时,请执行以下操作:





You just need to re-bind your GridView to reflect the changes you've made. So when you Update or Delete data, do something like this:

private void BindGrid(){          
      using (SqlConnection sqlConn = new SqlConnection
            (ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString)){
                string sql = "SELECT * FROM YourTableName";
                using(SqlCommand sqlCmd = new SqlCommand(sql,sqlConn)){
                    sqlConn.Open();
                    using(SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlCmd)){
                        sqlAdapter.Fill(dt);
                    }
                }
            }

            if(dt.Rows.Count > 0){
                GridView1.DataSource = dt;
                GridView1.DataBind();
            } 
}





然后在RowUpdating和RowDeleting事件中,你只需调用方法BindGrid()来刷新您的网格带有更改:





then at RowUpdating and RowDeleting events, you simply call the method BindGrid() to refresh your Grid with the changes:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e){
   //Your existing code
   BindGrid();
}







请注意以下事项:



(1)将你的连接字符串存储在你的web.config中,而不是在你的代码中硬编码。



(2)将参数传递给SQL查询时始终使用参数查询。以下是您可能需要查看的另一个参考:保护您的数据:防止SQL注入 [ ^ ]



(3)在处理吃资源的对象时,请记住使用使用块,例如:SqlConnection和SqlCommand




Please do take note of the following:

(1) Store your connection string in your web.config, rather than hard-coding it in your code behind.

(2) Always use Parameter Queries when passing parameters to your SQL query. Here's another reference that you may want to look at: Protect Your Data: Prevent SQL Injection[^]

(3) Remember to use the "Using Block" when dealing with objects that eat resources, such as: SqlConnection and SqlCommand


这篇关于使用多行文本框值检查数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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