3

好吧,我已经让这个程序在 SQL db 中插入新值,通过从组合框中的不同表之间进行选择。我需要更改 sql 查询,通过它我可以为组合框中的每个表单独使用插入命令......我需要帮助以粗体显示...

namespace combo
{
    public partial class Form1 : Form
    {
        List lstNewRows = new List();

        public Form1()
        {
            InitializeComponent();
        }
        private void PopulateComboBox()
        {
            try
            {

                List _items = new List();

                _items.Add("select * from lol");
                _items.Add("select * from datejoin");
                comboBox1.DataSource = _items;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PopulateComboBox();
        }

        private void PopulateGridView(string connString, string sqlQuery)
        {

            String strconnetcion = connString;

            SqlConnection con = new SqlConnection(strconnetcion);



            try
            {

                con.Open();

                SqlCommand sqlCmd = new SqlCommand();

                sqlCmd.Connection = con;
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = sqlQuery;

                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);

                dataGridView1.DataSource = dtRecord;
                dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                PopulateGridView(textBox1.Text, comboBox1.SelectedValue.ToString());
            }
        }

        private void InsertInfo()
        {

            string connectionString = null;
            SqlConnection connection;
            SqlDataAdapter adapter = new SqlDataAdapter();
            string value1 = "";
            string value2 = "";
            connectionString = @"Data Source=HP\SQLEXPRESS;database=MK;Integrated Security=true";
            connection = new SqlConnection(connectionString);
            foreach (int rowIndex in lstNewRows)
            {
                if (dataGridView1.Rows[rowIndex].Cells[0].Value != null && dataGridView1.Rows[rowIndex].Cells[1].Value != null)
                {

                    value1 = dataGridView1.Rows[rowIndex].Cells[0].Value.ToString();
                    value2 = dataGridView1.Rows[rowIndex].Cells[1].Value.ToString();
                    ***string sql = "insert into lol (name,marks) values('" + value1 + "','" + value2 + "')";***
                    try
                    {
                        connection.Open();
                        adapter.InsertCommand = new SqlCommand(sql, connection);
                        adapter.InsertCommand.ExecuteNonQuery();
                        MessageBox.Show("Row inserted !! ");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                    }

                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {


            InsertInfo();

        }

        private void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            lstNewRows.Add(e.Row.Index);
        }
    }
}
4

1 回答 1

2

浏览源代码后,我了解您要完成的工作。我将首先回答您的问题,但请在回答后阅读建议,因为按照您现在的方式进行操作,此应用程序最终可能会让维护它的人头疼。

答案

Change your items to be as follows:
List _items = new List();
_items.Add("lol"); // removing select * from 
_items.Add("datejoin"); // removing select * from 
comboBox1.DataSource = _items;

现在在您的PopulateGridView功能中,您可以sqlQuery

private void PopulateGridView(string connString, string sqlQuery) {
    sqlQuery = "select * from "+sqlQuery;

然后在您的InsertInfo函数中,您可以执行以下操作(在您定义字符串 sql 变量的位置):

string sql = string.Empty;
switch(comboBox1.SelectedValue) {
    case "lol":
        sql = "insert into lol (name,marks) values('" + value1 + "','" + value2 + "')";
        break;
    case "datejoin":
        sql = "insert into datejoin (..."; // fill in the column and values as needed
        break;
}

建议

  1. 请将您的普通字符串查询更改为存储过程或查看 LINQ
  2. 不要使用select *,总是尝试在选择查询中提及列名
  3. 在组合框中提供比表名更用户友好的名称,使表名对最终用户如此明显并不总是安全的
  4. 尝试使用单独的数据访问层并将数据库特定代码移动到该层
于 2013-03-08T09:40:29.057 回答