将数据插入数据库并使用C#在gridview中显示 [英] inserting data into database and display in gridview using C#

查看:105
本文介绍了将数据插入数据库并使用C#在gridview中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace EnableorDisableApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
 
            //FillData();
        }
 
 
        SqlConnection con = new SqlConnection("Server=KK-PC;Database=SampleDB;User id=sa;password=sa2008;");
 
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                con.Open();
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
 
        {
 
            SqlConnection con = new SqlConnection("Server=KK-PC;Initial Catalog=SampleDB;User id=sa;password=sa2008;");
            using (SqlCommand cmd = new SqlCommand("Insert into Emp_Details (Emp_ID,Name,Education) values (" + textBox2.Text + ",'" + textBox3.Text + "','" + textBox1.Text + "')", con))
            {
                cmd.ExecuteNonQuery();
            }
 
 
        }
 
 
      void FillData()
        {
            // 1
            // Open connection
            using (SqlConnection c = new SqlConnection("Server=KK-PC;Initial Catalog = SampleDB;User id=sa;Password=sa2008;"))
            {
                c.Open();
 
                // Create new DataAdapter
                using (SqlDataAdapter a = new SqlDataAdapter(
                    "SELECT * FROM Emp_Details", c))
                {
                    // 3
                    // Use DataAdapter to fill DataTable
                    DataTable t = new DataTable();
                    a.Fill(t);
                    // 4
                    // Render data onto the screen
                    dataGridView1.DataSource = t;
                }
            }
        }
 
        private void label2_Click(object sender, EventArgs e)
        {
 
        }
 
        private void label1_Click(object sender, EventArgs e)
        {
 
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {
 
        }
    }
}







数据显示在网格视图中,但是当我输入详细信息时,它不会插入到DB&网格视图。



任何人都可以帮我解决这个问题。




Data is displaying in grid view but when i enter the particulars it's not inserting in to DB & grid view.

Can any one help me tosolve this problem.

推荐答案

有很多错误我害怕的代码。



1.从GUI代码中获取SQL内容。阅读这篇文章 [ ^ ] eg。

2你的代码很容易被SQL注入。请参见此处 [ ^ ]

3)如果你想更新网格,你需要再次调用FillData插入后。网格的DataSource对象不会自行更新。

4)删除Form1,Button1等实例并用一些有用的名称替换它们。 (也改变相应的事件处理程序名称。)



希望这有帮助。
There is a lot wrong with the code I'm afraid.

1. Get the SQL stuff out of the GUI code. Read this article[^] eg.
2) You're code is vulnerable for SQL injection. See here[^]
3) If you want to update your grid, you need to call FillData again after an insert. The grid's DataSource object will not update itself.
4) Remove the Form1, Button1, etc... instances and replace them with some useful names. (also change the corresponding event handler names.)

hope this helps.


试试这个





try this


private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("User ID=sa;password=pass@word1;Initial Catalog=Test;Data Source=.;");
            con.Open();
            using (SqlCommand cmd = new SqlCommand("Insert into Emp_Details (Emp_ID,Name,Education) values (" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "')", con))
            {
                cmd.ExecuteNonQuery();
            }
            con.Close();

            FillData();
        }

        void FillData()
        {
            using (SqlConnection c = new SqlConnection("User ID=sa;password=pass@word1;Initial Catalog=Test;Data Source=.;"))
            {
                c.Open();
                using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Emp_Details", c))
                {
                    DataTable t = new DataTable();
                    a.Fill(t);
                    dataGridView1.DataSource = t;
                }
            }
        }


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace MyDBApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
        }
        SqlConnection con = new SqlConnection("Server=127.0.0.1;Database=SampleDB;User id=sa;password=sa2008;");

        
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                FillData();
            }
            catch (SystemException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            using (SqlCommand cmd = new SqlCommand("Insert into Emp_Details (Emp_ID,Name,Education) values (" + textBox2.Text + ",'" + textBox3.Text + "','" + textBox1.Text + "')", con))
            {
                cmd.ExecuteNonQuery();
            }
            FillData();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
    
    
     void FillData()
        {
            
                using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM Emp_Details", con))
                {
                    DataTable t = new DataTable();
                    a.Fill(t);
                    dataGridView1.DataSource = t;
                }
            
        }
    }
}


这篇关于将数据插入数据库并使用C#在gridview中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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