我正在尝试将此 VB.NET 代码转换为 C#:
Protected Sub BT_KEM_CARIAN_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BT_KEM_CARIAN.Click
Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("Select * from pengguna where idpengguna = @idpengguna", myconn)
cmd.CommandType = Data.CommandType.Text
cmd.Connection = myconn
Try
myconn.Open()
Dim rdr2 As SqlDataReader
cmd.Parameters.AddWithValue("idpengguna", KEMASKINI_MASUK.Text)
rdr2 = cmd.ExecuteReader(CommandBehavior.CloseConnection)
rdr2.Read()
KEMASKINI_ID.Text = rdr2("IDPENGGUNA").ToString
KEMASKINI_IC.Text = rdr2("NoIC").ToString()
KEMASKINI_NAMA.Text = rdr2("nama").ToString()
KEMASKINI_MASUK.Text = rdr2("idpengguna").ToString()
KEMASKINI_CAPAIAN.Text = rdr2("kodaccesslevel").ToString()
Catch
MsgBox("Succesfully Updated")
End Try
myconn.Close()
MultiView1.SetActiveView(View4)
End Sub
结果:
protected void BT_KEM_CARIAN_Click(object sender, System.EventArgs e)
{
System.Data.SqlClient.SqlConnection myconn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.AppSettings["DB"]);
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("Select * from pengguna where idpengguna = @idpengguna", myconn);
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = myconn;
try {
myconn.Open();
SqlDataReader rdr2 = default(SqlDataReader);
cmd.Parameters.AddWithValue("idpengguna", KEMASKINI_MASUK.Text);
rdr2 = cmd.ExecuteReader(CommandBehavior.CloseConnection);
rdr2.Read();
KEMASKINI_ID.Text = rdr2("IDPENGGUNA").ToString;
KEMASKINI_IC.Text = rdr2("NoIC").ToString();
KEMASKINI_NAMA.Text = rdr2("nama").ToString();
KEMASKINI_MASUK.Text = rdr2("idpengguna").ToString();
KEMASKINI_CAPAIAN.Text = rdr2("kodaccesslevel").ToString();
} catch {
Interaction.MsgBox("Succesfully Updated");
}
myconn.Close();
MultiView1.SetActiveView(View4);
}
下面的代码给出了错误
rdr2 是一个“变量”,但用作“方法”
KEMASKINI_ID.Text = rdr2("IDPENGGUNA").ToString;
KEMASKINI_IC.Text = rdr2("NoIC").ToString();
KEMASKINI_NAMA.Text = rdr2("nama").ToString();
KEMASKINI_MASUK.Text = rdr2("idpengguna").ToString();
KEMASKINI_CAPAIAN.Text = rdr2("kodaccesslevel").ToString();