c#datagridview数据库操作

来源:互联网 发布:拼音读法软件 编辑:程序博客网 时间:2024/05/22 12:44

  private void button2_Click(object sender, EventArgs e)

        {

            SqlConnection conn = null;

            SqlCommand comm = null;

            DataSet Ds = null;

            SqlDataAdapter Dr = null;

            string uri = @"server=localhost;database=Warehouse;uid=sa;pwd=123456";

            conn = new SqlConnection(uri);

            conn.Open();

            comm = new SqlCommand();

            comm.Connection = conn;

            string sql = "select goodId ,goodName ,goodPrice,Count from good1";

            comm.CommandType = CommandType.Text;

            comm.CommandText = sql;

            Dr = new SqlDataAdapter();

            Dr.SelectCommand = comm;

            Ds = new DataSet();

            Dr.Fill(Ds, "good1");

            Dr = new SqlDataAdapter();

            Dr.SelectCommand = comm;

Ds = new DataSet();

Dr.Fill(Ds, "good1");

dataGridView1.DataSource =

                     Ds.Tables["good1"].DefaultView;

int AllRows = Ds.Tables["good1"].Rows.Count;

double sum = 0;

for (int i = 0; i < AllRows; i++) {

    int num = int.Parse(Ds.Tables["good1"].Rows[i][3].ToString());

    double price1 = double.Parse(Ds.Tables["good1"].Rows[i][2].ToString());

    sum = sum + num * price1;

}

textBox1_TalCost.Text = sum.ToString();

原创粉丝点击