Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过从组合框中选择不同的表,在sql db..中插入新值_C#_Sql Server - Fatal编程技术网

C# 通过从组合框中选择不同的表,在sql db..中插入新值

C# 通过从组合框中选择不同的表,在sql db..中插入新值,c#,sql-server,C#,Sql Server,我制作了这个程序,通过在组合框中的不同表之间进行选择,在SQL db中插入新值。我需要更改SQL查询,通过该查询,我可以对组合框中的每个表单独使用insert命令…我需要帮助的区域以粗体显示 namespace combo { public partial class Form1 : Form { List lstNewRows = new List(); public Form1() { Initiali

我制作了这个程序,通过在组合框中的不同表之间进行选择,在SQL db中插入新值。我需要更改SQL查询,通过该查询,我可以对组合框中的每个表单独使用insert命令…我需要帮助的区域以粗体显示

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);
        }
    }
}

在阅读了源代码之后,我理解了您试图实现的目标。我将首先回答您的问题,但请在回答后阅读建议,因为按照您现在的方式继续此应用程序可能会让维护它的人非常头疼

回答

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变量的位置):

建议

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;
  • 请将普通字符串查询更改为存储过程或查看LINQ
  • 不要使用
    select*
    ,始终尝试在select查询中提及列名
  • 在组合框中提供一个比表名更为用户友好的名称,使表名对最终用户如此明显并不总是安全的
  • 尝试使用单独的数据访问层,并将特定于数据库的代码移到该层中

  • 没有粗体字。我不清楚你的问题,也没有粗体字。您是否正在尝试调用一组SQL,这些SQL将根据参数插入到不同的表中?或者使用C代码根据参数调用不同的SQL?Tom.Bowen89抱歉,语句介于*嗯,我想使用C代码根据参数调用不同的SQL…请帮助我想这应该是粗体文本:
    ***string SQL=“插入lol(名称、标记)值(“+value1+”,“+value2+”)”***