C# c语言中的第一个代码#

C# c语言中的第一个代码#,c#,C#,我是C#的新手。我已经写了一个小代码。你能帮我吗 这是我的密码: private void button1_Click(object sender, EventArgs e) { string povezava = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Matic\\Documents\\FERI\\2.LETNIK\\1.SEMESTER\\RPS\\VAJA3\\bazaPodatkov.accdb";

我是C#的新手。我已经写了一个小代码。你能帮我吗 这是我的密码:

private void button1_Click(object sender, EventArgs e)
{
    string povezava = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Matic\\Documents\\FERI\\2.LETNIK\\1.SEMESTER\\RPS\\VAJA3\\bazaPodatkov.accdb";
    string zapisi = "update Zaposleni set ID='" + this.textBox1.Text + 
"',Ime='" + this.textBox2.Text + 
"',Priimek='" + this.textBox3.Text + 
"',Uporabnisko_ime='" + this.textBox4.Text + 
"',Geslo='" + this.textBox5.Text + 
"',E_posta='" + this.textBox6.Text + 
"',Ulica='" + this.textBox7.Text + 
"',Hisna_stevilka='" + this.textBox8.Text + 
"',Mesto='" + this.textBox9.Text + 
"',Delovno_mesto='" + this.textBox10.Text + 
"' where ID='" + this.textBox1.Text + "';";

    OleDbConnection baza = new OleDbConnection(povezava);
    OleDbCommand beri = new OleDbCommand(zapisi, baza);
    OleDbDataReader branje;
    try
    {
        baza.Open();
        branje = beri.ExecuteReader();
        MessageBox.Show("Podatki so shranjeni.");
        while (branje.Read())
        {

        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
条件表达式中存在数据类型不匹配


你看到我的代码有什么错误吗

您的命令字符串(
zapisi
)有一个额外的分号结尾,请删除它,然后重试


也考虑使用

命令字符串中有一个额外的分号末尾(<代码> ZAPISI )删除它并重试。< /P>

也考虑使用

命令字符串中有一个额外的分号末尾(<代码> ZAPISI )删除它并重试。< /P>

也考虑使用

命令字符串中有一个额外的分号末尾(<代码> ZAPISI )删除它并重试。< /P>

也考虑使用

您的代码包含一些可见的错误,可能还有一些不可见的错误。 第一个问题。 永远不要使用字符串连接来构建sql查询。始终使用参数化查询。
将占位符(?for
OleDb/Access
)放在要放置值的位置,然后构建参数集合,在其中设置值。框架代码用参数的值替换占位符,在值周围加上适当的引号,并消除sql注入攻击的可能性()

其次,您有一个更新查询,那么返回一个
OleDbDataReader

只需执行命令(通过调用
ExecuteNonQuery
)并最终检查查询是否更新了任何行

第三,用using语句括住
OleDbConnection/Command
,以确保正确关闭和清理连接和命令

private void button1_Click(object sender, EventArgs e)
{
    string povezava = "........."
    string zapisi = "update Zaposleni set ID=?,Ime=?,Priimek=?,Uporabnisko_ime=?,Geslo=?," + 
                    "E_posta=?,Ulica=?,Hisna_stevilka=?,Mesto=?,Delovno_mesto=? " + 
                    "where ID=?";
    try
    {
        using(OleDbConnection baza = new OleDbConnection(povezava))
        using(OleDbCommand beri = new OleDbCommand(zapisi, baza))
        {
            baza.Open();
            beri.Parameters.AddWithValue("@p1",this.textBox1.Text);
            beri.Parameters.AddWithValue("@p2",this.textBox2.Text);
            beri.Parameters.AddWithValue("@p3",this.textBox3.Text);
            .... and so on for the other parameters
            beri.Parameters.AddWithValue("@p11",this.textBox1.Text);
            int rowsAffected = beri.ExecuteNonQuery();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
最后是看不见的错误。将所有值作为字符串传递。仅当接收值的数据库字段是文本字段时,此选项才正确。如果一个或多个字段不是字符串(例如日期或整数),则尝试将其值设置为字符串会触发条件表达式中的数据类型不匹配错误消息

如果是这种情况,则需要将字符串转换为适当的数据类型。 例如(假设ID字段是数字字段)


您的代码包含一些可见错误,也可能包含一些不可见错误

第一个问题。 永远不要使用字符串连接来构建sql查询。始终使用参数化查询。
将占位符(?for
OleDb/Access
)放在要放置值的位置,然后构建参数集合,在其中设置值。框架代码用参数的值替换占位符,在值周围加上适当的引号,并消除sql注入攻击的可能性()

其次,您有一个更新查询,那么返回一个
OleDbDataReader

只需执行命令(通过调用
ExecuteNonQuery
)并最终检查查询是否更新了任何行

第三,用using语句括住
OleDbConnection/Command
,以确保正确关闭和清理连接和命令

private void button1_Click(object sender, EventArgs e)
{
    string povezava = "........."
    string zapisi = "update Zaposleni set ID=?,Ime=?,Priimek=?,Uporabnisko_ime=?,Geslo=?," + 
                    "E_posta=?,Ulica=?,Hisna_stevilka=?,Mesto=?,Delovno_mesto=? " + 
                    "where ID=?";
    try
    {
        using(OleDbConnection baza = new OleDbConnection(povezava))
        using(OleDbCommand beri = new OleDbCommand(zapisi, baza))
        {
            baza.Open();
            beri.Parameters.AddWithValue("@p1",this.textBox1.Text);
            beri.Parameters.AddWithValue("@p2",this.textBox2.Text);
            beri.Parameters.AddWithValue("@p3",this.textBox3.Text);
            .... and so on for the other parameters
            beri.Parameters.AddWithValue("@p11",this.textBox1.Text);
            int rowsAffected = beri.ExecuteNonQuery();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
最后是看不见的错误。将所有值作为字符串传递。仅当接收值的数据库字段是文本字段时,此选项才正确。如果一个或多个字段不是字符串(例如日期或整数),则尝试将其值设置为字符串会触发条件表达式中的数据类型不匹配错误消息

如果是这种情况,则需要将字符串转换为适当的数据类型。 例如(假设ID字段是数字字段)


您的代码包含一些可见错误,也可能包含一些不可见错误

第一个问题。 永远不要使用字符串连接来构建sql查询。始终使用参数化查询。
将占位符(?for
OleDb/Access
)放在要放置值的位置,然后构建参数集合,在其中设置值。框架代码用参数的值替换占位符,在值周围加上适当的引号,并消除sql注入攻击的可能性()

其次,您有一个更新查询,那么返回一个
OleDbDataReader

只需执行命令(通过调用
ExecuteNonQuery
)并最终检查查询是否更新了任何行

第三,用using语句括住
OleDbConnection/Command
,以确保正确关闭和清理连接和命令

private void button1_Click(object sender, EventArgs e)
{
    string povezava = "........."
    string zapisi = "update Zaposleni set ID=?,Ime=?,Priimek=?,Uporabnisko_ime=?,Geslo=?," + 
                    "E_posta=?,Ulica=?,Hisna_stevilka=?,Mesto=?,Delovno_mesto=? " + 
                    "where ID=?";
    try
    {
        using(OleDbConnection baza = new OleDbConnection(povezava))
        using(OleDbCommand beri = new OleDbCommand(zapisi, baza))
        {
            baza.Open();
            beri.Parameters.AddWithValue("@p1",this.textBox1.Text);
            beri.Parameters.AddWithValue("@p2",this.textBox2.Text);
            beri.Parameters.AddWithValue("@p3",this.textBox3.Text);
            .... and so on for the other parameters
            beri.Parameters.AddWithValue("@p11",this.textBox1.Text);
            int rowsAffected = beri.ExecuteNonQuery();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
最后是看不见的错误。将所有值作为字符串传递。仅当接收值的数据库字段是文本字段时,此选项才正确。如果一个或多个字段不是字符串(例如日期或整数),则尝试将其值设置为字符串会触发条件表达式中的数据类型不匹配错误消息

如果是这种情况,则需要将字符串转换为适当的数据类型。 例如(假设ID字段是数字字段)


您的代码包含一些可见错误,也可能包含一些不可见错误

第一个问题。 永远不要使用字符串连接来构建sql查询。始终使用参数化查询。
将占位符(?for
OleDb/Access
)放在要放置值的位置,然后构建参数集合,在其中设置值。框架代码用参数的值替换占位符,在值周围加上适当的引号,并消除sql注入攻击的可能性()

其次,您有一个更新查询,那么返回一个
OleDbDataReader

只需执行命令(通过调用
ExecuteNonQuery
)并最终检查查询是否更新了任何行

第三,将
oledbconnect括起来