您好我将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);
答案 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
代码转换器:
答案 2 :(得分:1)
如果您的Listbox
只有一个选定项目,请将您的代码更改为
cmd.Parameters.AddWithValue("@Id_Sh", this.ListBox1.SelectedItem.ToString);
如果您的Listbox
包含多个所选项目且SelectionMode
为MultiExtended
,则可以使用ListBox.SelectedIndices
代替;
cmd.Parameters.AddWithValue("@Id_Sh", this.ListBox1.SelectedIndices[0].ToString);