在运行时在gridview中添加值并将其保存到数据库中? [英] Add values in gridview at runtime and save it to database?

查看:132
本文介绍了在运行时在gridview中添加值并将其保存到数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i需要在运行时在我的网格视图中添加值并将其保存到数据库。我尝试了各种各样的事情,比如创建一个函数和调用和插入值但在运行时这不起作用。我是这个领域的新手,不太了解这个..请帮助。



我尝试过:



Hi,

i need to add values in my Grid-View at Run time and save it to the database.i tried various things like by creating a function and calling and inserting values but at run-time this Does not work. i am novice in this field and Don't know much about this ..please help.

What I have tried:

public void Fill_grid()
     {
         SqlCommand cmd = con.CreateCommand();
         cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from DBTable";
         cmd.ExecuteNonQuery();
         DataTable dt = new DataTable();
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         da.Fill(dt);
         dataGridView1.DataSource = dt;

     }

     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
     {
         id = dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString();
         if (id == "")
         {
             id1 = 0;
         }
         else
         {
             id1 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString());
         }

         if (id1 == 0)
         {
             SqlCommand cmd = con.CreateCommand();
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = "Insert into DBTable values('" + dataGridView1.Rows[e.RowIndex].Cells["FileName"].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells["Sequence"].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells["Delay"].Value.ToString() + "')";
             cmd.ExecuteNonQuery();
             Fill_grid();
         }

         else
         {
             SqlCommand cmd = con.CreateCommand();
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = "update DBTable set Filename='" + dataGridView1.Rows[e.RowIndex].Cells["Filename"].Value.ToString() + "',Sequence='" + dataGridView1.Rows[e.RowIndex].Cells["Sequence"].Value.ToString() + "',Delay='" + dataGridView1.Rows[e.RowIndex].Cells["Delay"].Value.ToString() + "' where id=" + id1 + "";
             cmd.ExecuteNonQuery();
             Fill_grid();

         }

     }

推荐答案

不起作用这不是一个有用的错误报告 - 它完全没有告诉我们发生了什么。



从修复代码开始:永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



之后,很多这将取决于您的数据和用户输入 - 我们无法访问其中任何一个!

所以,它取决于你。

dataGridView1_CellValueChanged 函数的第一行放置一个断点,并通过调试器运行您的代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。


对不起,但我们不能为你做到这一点 - 时间让你学习一门新的(非常非常有用的)技能:调试!
"Does not work" is not a helpful error report - it tells us absolutely nothing about what is happening.

Start by fixing your code: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

After that, a lot of this is going to be down to your data and user input - and we have no access to either of those!
So, its going to be up to you.
Put a breakpoint on the first line in the dataGridView1_CellValueChanged function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于在运行时在gridview中添加值并将其保存到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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