将VB代码转换为C#时出错

时间:2013-10-27 15:48:07

标签: c# .net vb.net

您好我将vb转换为c sharp

Dim Query As String = "UPDATE F_Shqip SET Lika_Sh=@Fjalet_Sh WHERE Id_Sh=@Id_Sh"
            Dim cmd As SqlCommand = New SqlCommand(Query, con)

            cmd.Parameters.AddWithValue("@Lika_Sh", Trim(Me.txbLikaSh.Text))
            cmd.Parameters.AddWithValue("@Id_Sh", Me.ListBox1.SelectedItem(0).ToString)

简明扼要:

string Query = "UPDATE F_Shqip SET Lika_Sh=@Lika_Sh WHERE Id_Sh=@Id_Sh";
                SqlCommand cmd = new SqlCommand(Query, con);

//two lines with errors
                cmd.Parameters.AddWithValue("@Lika_Sh", Strings.Trim(this.txbLikaSh.Text));
                cmd.Parameters.AddWithValue("@Id_Sh", this.ListBox1.SelectedItem(0).ToString);

3 个答案:

答案 0 :(得分:1)

尝试this.ListBox1.SelectedItem[0]而不是this.ListBox1.SelectedItem(0)
C#对数组使用方括号,但VB.NET使用括号表示数组。

答案 1 :(得分:1)

而不是

cmd.Parameters.AddWithValue("@Lika_Sh", Strings.Trim(this.txbLikaSh.Text));
cmd.Parameters.AddWithValue("@Id_Sh", this.ListBox1.SelectedItem(0).ToString);

试试这个:

cmd.Parameters.AddWithValue("@Lika_Sh", this.txbLikaSh.Text.Trim());
cmd.Parameters.AddWithValue("@Id_Sh", this.ListBox1.SelectedItem[0].ToString());

以下链接可能有用:

CodeProject - Complete Comparison for VB.NET and C#

Wikipedia - Comparison of C Sharp and Visual Basic .NET

代码转换器:

DeveloperFusion - Convert VB.NET to C#

DeveloperFusion - Convert C# to VB.NET

答案 2 :(得分:1)

如果您的Listbox只有一个选定项目,请将您的代码更改为

cmd.Parameters.AddWithValue("@Id_Sh", this.ListBox1.SelectedItem.ToString);

如果您的Listbox包含多个所选项目且SelectionModeMultiExtended,则可以使用ListBox.SelectedIndices代替;

cmd.Parameters.AddWithValue("@Id_Sh", this.ListBox1.SelectedIndices[0].ToString);