不要在localDataBace中显示第二次插入的插入

时间:2018-10-03 07:34:59

标签: c# .net

我想将一些数据插入localdatabace并成功完成插入,但是没有在datagridview的第二次插入中显示debog,我称之为全选插入结束并看不到最后插入的内容,但是当我插入下一个数据时,将显示最后的数据,每个人都可以帮助我吗?

public void connect()
{
    String conString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\hana\\documents\\visual studio 2017\\Projects\\Bank\\Bank\\Database.mdf;Integrated Security=True";

    SqlConnection sql = new SqlConnection(conString);
    String sqll = "Insert into TblBank (txtTodayDate" +
        ",txtSahebanHesab" +
        ",txtShobe" +
        ",txtShomareMoshtari" +
        ",txtShoareHesab" +
        ",cmbNoeHesab" +
        ",txtSarresid" +

        ")";
    try
    {
        sql.Open();

        SqlDataAdapter sda = new SqlDataAdapter(sqll, sql);
        SqlCommand sc = new SqlCommand(sqll,sql);

        sc.Parameters.AddWithValue("todayDate", new PersianDateTime(dtpTodayDate.the_date).ToString("yyyy/MM/dd"));
        sc.Parameters.AddWithValue("sahebanHesab", txtSahebHesabName.Text);
        sc.Parameters.AddWithValue("shobe", txtshobe.Text);
        sc.Parameters.AddWithValue("shomareMoshtari", txtShomareMoshtari.Text);
        sc.Parameters.AddWithValue("shoareHesab", txtShomareHesab.Text);
        sc.Parameters.AddWithValue("noeHesab", cmbNoeHesab.SelectedIndex);
        sc.Parameters.AddWithValue("sarresid", txtSarResidMah.Text);

        sc.ExecuteNonQuery();        
        sql.Close();
        select();
    }
    catch (Exception ex)
    {

    }
}

并选择为

private void select()
{
    String conString = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\hana\\documents\\visual studio 2017\\Projects\\Bank\\Bank\\Database.mdf;Integrated Security=True";
    SqlConnection cn = new SqlConnection(conString);
    String sqlString = "SELECT * FROM TblBank Order BY Id desc ";
    SqlConnection sql = new SqlConnection(conString);
    SqlCommand cmd = new SqlCommand(sqlString, cn);
    try {
        sql.Open();
    SqlDataAdapter sa = new SqlDataAdapter(sqlString, sql);
    using (SqlDataReader read = sa.SelectCommand.ExecuteReader())
    {
        if (read.Read())
        {
            DataTable dt = new DataTable();
            dt.Load(read);
            this.tblBankDataGridViewX.DataSource = dt;
        }

    }
        sql.Close();
    }
    catch (Exception ex)
    {

    }
}

1 个答案:

答案 0 :(得分:2)

DataReader.Read()方法将迭代DataTable.Load()之前的结果集。由于DataReader是仅转发流,因此在执行DataTable.Load()并将DataTable设置为DataSource时,DataGridView的内容仍然为空。尝试使用DataReader.HasRows属性来检查结果集的可用性:

using (SqlConnection cn = new SqlConnection(conString))
{
     using (SqlCommand cmd = new SqlCommand(sqlString, cn))
     {
        try 
        {
            cn.Open();
            using (SqlDataReader read = cmd.ExecuteReader())
            {
                // check if the reader returns result set
                if (read.HasRows)
                {
                    DataTable dt = new DataTable();
                    dt.Load(read);
                    this.tblBankDataGridViewX.DataSource = dt;
                }

            }
        }
        catch (Exception ex)
        {
            // do something
        }
     }
}