从GridView持久化数据 [英] Persisting Data from a GridView

查看:76
本文介绍了从GridView持久化数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果其中包含一些控件,如DropDownLists,TextBoxes,RadioButtons,如何从GridView上载数据并将其存储在数据库中?

这是代码
当按钮A单击以将数据从GridView上载到数据库时

How do you upload the data From GridView and store in database if we have some controls like DropDownLists,TextBoxes,RadioButtons included in it?

Here Is Code
When A of Button Click to upload the data to Database from GridView

protected void Button2_Click(object sender, EventArgs e)
    {
        int i;
        for (i = 0; i < gridview2.Rows.Count; i++)
        {
            SqlCommand cmd = new SqlCommand("insert into rep values('"+gridview2.Rows[i].Cells[0].Text+"','" + gridview2.Rows[i].Cells[1].Text +"','" + gridview2.Rows[i].Cells[2].Text+ "','"+ gridview2.Rows[i].Cells[3].Text+ "')", con);

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

        }




这里+ gridview2.Rows [i] .Cells [2] .Text +是一个下拉列表



+ gridview2.Rows [i] .Cells [3] .Text +是一个TextBoxes


请发布答案.




Here + gridview2.Rows[i].Cells[2].Text+ is a dropdownlist

and

+gridview2.Rows[i].Cells[3].Text+ is a TextBoxes


Please post the answers.

推荐答案

con似乎不在此范围内,除非您在表单级别使用它.这不是很好的做法.

我建议建立对象来存储值,然后将该对象集合传递给负责写出数据的方法.

在列表中建立命令对象.打开数据库,然后执行命令,然后关闭数据库.

如果此数据对于原子存储至关重要,则应将操作包装在事务中.

您还应该使用try...catch块.

另外,考虑一个DAL框架,它可以为您解决大部分此类问题.

干杯.
con doesn''t appear to be in this scope, unless you''re using it at a form level. Which is not really good practice.

I would recommend building up objects to store the values, then passing that object collection to a method that takes care of writing the data out.

Build up your command objects in a list. Open the DB, then execute the commands, then close the DB.

If this data is critical to be stored atomically you should wrap the operation in a transaction.

You should also use a try...catch block.

Alternatively, consider a DAL framework which takes care of most of this for you.

Cheers.


好吧,对于初学者来说,这是一种学习的东西,在这种情况下,您不应该在形成查询时尝试直接提取值.

而是将其分为两个步骤.首先,在一个局部变量中获取所有值,然后在第二步中,在查询中使用这些变量.

随着学习的深入,您将了解有关参数化查询和对象模型的信息,它们将为您提供更多帮助.
Well, for learning stuff (as beginners), in such cases, you shouldn''t try to extract the values directly while formming query.

Instead, break it into two steps. First one, get all the values in a local variable or so and then in second step, use those variables in your query.

As you learn more, you will come to know about parameterized queries and object models that will help more.


以James和Sandeep先生所说的为基础,我注意到这是一个Web应用程序.您还应该考虑保护数据库免受SQL注入,并停止在代码中使用嵌入式SQL语句.相反,构建一个存储过程并在SQLCommand中调用该存储过程,并通过SQL参数传递数据值.
Building on what Mr James and Sandeep said, I noticed that this is a web application. You should also take into consideration protecting your database against SQL injections and discontinue the use of embedded SQL statements in your code. Instead, build a stored procedure and call that in your SQLCommand, passing in your data values via SQL Parameters.


这篇关于从GridView持久化数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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