本文介绍了C#Sql列名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void button4_Click_1(object sender, EventArgs e)
    {
        string s = textBox1.Text;

        string s1 = comboBox1.Text;
        string s2 = comboBox2.Text;
        SqlCeConnection conn = new SqlCeConnection(@"Data Source=D:\Desktop\DB2\DB2\Database1.sdf");
        try
        {
            conn.Open();
            SqlCeCommand cmd = new SqlCeCommand(" update Kambariai set Klientas=[s]  Where [Kambario rūšis]=[s1]  ", conn);
            cmd.ExecuteNonQuery();
            toolStripStatusLabel1.Text = "Duomenys įrašyti";
            conn.Close();
        }
        catch (Exception ee)
        {
            MessageBox.Show(ee.Message);
        }
    }



我想通过更新 Klientas 价值与 textbox1.Text 这是字符串= S 。它应该罚款,SQL,但我得到一个错误说,列名无效列= S1 S1 不应该有针对性的列名应作为列行值。

I am trying to update my datatable by updating Klientas value with textbox1.Text which is made to string = s. It should work fine as Sql But I get an error saying that The column name is not valid Column = s1. s1 shouldn't be targeted as column name it should be used as column row value.

这是过时的形象Kliento ID更改为Klientas

This is outdated image Kliento ID is changed to Klientas

推荐答案

试试这个:

SqlCeCommand cmd = new SqlCeCommand("update Kambariai set Klientas="+s+"  Where [Kambario rūšis]='"+s1+"' ", conn);



分析:

从你都试过了, CMD 有值,如:

From what you have tried, cmd has value like :

update Kambariai set Klientas=s Where [Kambario rūšis]=s1

这是通过把合适的双人和单人周围的引号,该值将是这样的:

From by putting proper double and single quotes around it, the value would be like:

update Kambariai set Klientas=1 Where [Kambario rūšis]='bar'

侧面说明:

,因为它增加了SQL注入的风险我不会推荐这种方法。而不是使用参数化查询。

I would not recommend this method since it increases the risk of SQL injection. Use parameterized query instead.

这篇关于C#Sql列名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:08