winforms 为什么我不能显示数据

vs91vp4v  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(160)

当我运行代码时,所需的信息没有出现在网格框中。

{
    public partial class fatura : Form
    {
        SqlConnection baglanti = new SqlConnection("Server=localhost;Database=master; Trusted_Connection=True");
        DataTable tablo;
        araba_ekleme araba_ekleme = new araba_ekleme();
        public fatura()
        {
            InitializeComponent();
        }
        public DataTable listele(SqlDataAdapter adtr, string sorgu)
        {
            tablo = new DataTable();
            adtr = new SqlDataAdapter(sorgu, baglanti);
            adtr.Fill(tablo);
            baglanti.Close();
            return tablo;
        }
        private void onay_bekleyen()
        {
            string cumle = "declare @tc_no bigint select tc_no,car_plate,total,date,tax from [invoice] WHERE (status=0) AND tc_no=@tc_no";
            SqlDataAdapter adtr2 = new SqlDataAdapter();
            dg_pending.DataSource = listele(adtr2, cumle);
        }
        private void onaylanan()
        {
            string cumle = "declare @tc_no bigint select tc_no,car_plate,total,date,tax from [invoice] WHERE (status=1) AND tc_no=@tc_no";
            SqlDataAdapter adtr2 = new SqlDataAdapter();
            dg_done.DataSource = listele(adtr2, cumle);
        }
        private void fatura_Load(object sender, EventArgs e)
        {
            onay_bekleyen();
            onaylanan();
        }
    }
}

当我运行这段代码时,它给出了一个错误。string cumle = "select tc_no,car_plate,total,date,tax from [invoice] WHERE (status=0) AND tc_no=@tc_no";
所以我使用了这个代码块string cumle = "declare @tc_no bigint select tc_no,car_plate,total,date,tax from [invoice] WHERE (status=0) AND tc_no=@tc_no";,当我运行代码时,所需的信息没有出现在网格框中。

yrefmtwq

yrefmtwq1#

编辑(SqlDataAdapter adtr, string sorgu)函数,如下所示:
1-您不需要发送SqlDataAdapter as input并在函数内部定义它。
2-必须打开连接。
3-Use using替换为SqlDataAdapter and SqlConnection,因此不需要处理。
4-使用try- catch捕获错误(在下面的函数中,如果有错误,它将显示为一个meesageBox。您可以将错误写入文件、控制台或其他任何地方)

public DataTable listele(string sorgu)
    {
        DataTable tablo = new DataTable();
        using (SqlConnection MyConnnection = new SqlConnection(conString ))
        {
            using (SqlDataAdapter dataAdapter = new SqlDataAdapter(sorgu, MyConnnection))
            {
                try
                {
                    MyConnnection.Open();
                    dataAdapter.Fill(tablo);
                }catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        return tablo;
    }

定义connection string

public partial class fatura : Form
{
    public string conString = "Server=localhost;Database=master; Trusted_Connection=True";
    SqlConnection baglanti = new SqlConnection("Server=localhost;Database=master; Trusted_Connection=True");
    DataTable tablo;
    araba_ekleme araba_ekleme = new araba_ekleme();
    ...

调用函数如下:

dg_pending.DataSource = listele(cumle);

相关问题