如何在C#中编写此VB代码SQLDatareader有问题 [英] How to write this VB code in C# I have a problem with SQLDatareader

查看:88
本文介绍了如何在C#中编写此VB代码SQLDatareader有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C#编写此代码时遇到问题.这给了我一个错误,那就是SQLDatareader不能用作构造函数.
请提供完整转换编码的 urgent 答案.

----------------------- VB.NET--编码-------------------- -------------

I have a problem when I''m writing this code in C#. It was giving me the error that SQLDatareader cannot be used as a constructor.
Please provide me urgent answer with full converted coding.

-----------------------THE VB.NET--CODING---------------------------------

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd As New SqlCommand("select * from Exam_master", con)
        If con.State = ConnectionState.Closed Then
            con.Open()
        End If

        dr = cmd.ExecuteReader()
        While dr.Read()
            If dr("Stud_id") = TextBox1.Text Then
                Label16.Text = ""
                Label8.Text = dr("Stud_id")
                Label10.Text = dr("Course_id")
                Label9.Text = dr("Name")
                Label14.Text = dr("Class")
                Label11.Text = dr("Maximum")
                Label12.Text = dr("Minimum")
                Label13.Text = dr("Total")
                Exit While
            Else
                Label16.Text = "Invalid Student......"
                Label8.Text = ("-------------")
                Label10.Text = ("-------------")
                Label9.Text = ("-------------")
                Label14.Text = ("-------------")
                Label11.Text = ("-------------")
                Label12.Text = ("-------------")
                Label13.Text = ("-------------")
            End If
        End While
    End Sub

推荐答案

将VB转换为C#或将C#转换为VB


您必须先声明变量,然后才能在C#.

You have to declare variables before using them in C#.

DataReader dr = cmd.ExecuteReader();



应该适合您.



should work for you.


尝试一下:

Try this:

protected void Button1_Click(object sender, System.EventArgs e)
{
    SqlCommand cmd = new SqlCommand("select * from Exam_master", con);
    if (con.State == ConnectionState.Closed) {
        con.Open();
    }

    dr = cmd.ExecuteReader();
    while (dr.Read()) {
        if (dr("Stud_id") == TextBox1.Text) {
            Label16.Text = "";
            Label8.Text = dr("Stud_id");
            Label10.Text = dr("Course_id");
            Label9.Text = dr("Name");
            Label14.Text = dr("Class");
            Label11.Text = dr("Maximum");
            Label12.Text = dr("Minimum");
            Label13.Text = dr("Total");
            break; // TODO: might not be correct. Was : Exit While
        } else {
            Label16.Text = "Invalid Student......";
            Label8.Text = ("-------------");
            Label10.Text = ("-------------");
            Label9.Text = ("-------------");
            Label14.Text = ("-------------");
            Label11.Text = ("-------------");
            Label12.Text = ("-------------");
            Label13.Text = ("-------------");
        }
    }
}



希望对您有所帮助:)



hope it helps :)


这篇关于如何在C#中编写此VB代码SQLDatareader有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆