`

C#与SQLServer

    博客分类:
  • VS
 
阅读更多

C#操作SQLServer

                    string sql = "select * from " + damls[0] + " where ndu like '%" + textBox1.Text + "%' and jgname like '%" + textBox2.Text + "%' and qx like '%" + textBox3.Text + "%'";
                    string connstring = @"server=192.168.1.195\SQL2008;database=6.0;user id=sa;password=Sql2008";
                    SqlConnection conn = new SqlConnection(connstring);
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = sql;
                    cmd.Connection = conn;
                    //不查询操作cmd.ExecuteNonQuery();
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    conn.Open();
                    DataSet ds = new DataSet();
                    da.Fill(ds, "数据导出");
                    System.Data.DataTable dt = ds.Tables[0];
                    //遍历所有的行
                    foreach (DataRow dr in dt.Rows)
                    {
                        //遍历所有的列
                        foreach (DataColumn dc in dt.Columns)
                        {
                            if (dc.ColumnName.Equals("ID") || dc.ColumnName.Equals("id"))
                            {
                                ajids += "'" + dr[dc] + "',";
                            }
                        }
                    }
                    if (!ajids.Equals(""))
                    {
                        ajids = ajids.Substring(0, ajids.Length - 1);
                    }
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics