当前位置:编程学习 > C#/ASP.NET >>

C#代码小问题

DataGridView里面显示的问题;

private void BasicallySearch()
        {
            string sql = "";
            // 查询语句的前半部分
            sql = "SELECT Id,NickName,Age,Sex FROM Users";
           
            // 精确查找
            if (rdoNicetySearch.Checked == true)
            {
                if (txtLoginId.Text.Trim() == "" && txtNickName.Text.Trim() == "")
                {
                    MessageBox.Show("还没有填查询条件呢!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                // 按帐号查找
                else if (txtLoginId.Text.Trim() != "") 
                {
                    sql += string.Format(" WHERE Id={0}", int.Parse(txtLoginId.Text.Trim()));
                }
                // 按昵称查找
                else
                {
                    sql += string.Format(" WHERE NickName like '%{0}%'", txtNickName.Text.Trim());
                 }               
            }
           
            // 重新填充DataSet
            dataSet.Tables[0].Clear();
            dataAdapter.SelectCommand.CommandText = sql;
            dataAdapter.Fill(dataSet,"Users");

            // 设置控件可见的属性
            // 调整显示结果的panel的位置,让它和显示条件的Panel的位置相同
            pnlBaseResult.Location = pnlBaseCondition.Location;
            // 使显示结果的panel可见
            pnlBaseResult.Visible = true;
            btnAdd.Visible = true;  //“加为好友”按钮可见
            btnBack.Visible = true;   //“上一步”按钮可见
        }

这个代码的意思:dataAdapter.SelectCommand.CommandText = sql;

和dataAdapter.CommandText = sql;意思一样不,有什么区别

补充:dataAdapter.CommandText = sql;

就是sd.CommandText = sql;

笔误都没看都来,晕,

你直接说sd.CommandText = sql;和

sd.SelectCommand.CommandText = sql;

有什么不同

追问:我要在下拉框显示血型和星座,要写2行查询的代码就要用它。

dataAdapter.CommandText = sql;

如:

string sql="";

sql="select star from star"; //查星座

class1.con.Open();

sqlcommand sm=new sqlcommand(sql,class1.con);

sqlDataRead sd=sm.Executereader();

while(sd.Read())

{

ComboBox1.Items.Add(sd[0].Tostring());

}

sd.close();

sql="select getbood where getbood";//查血型

dataAdapter.CommandText = sql;

sd=sm.ExecuteReader();

while(sd.Read())

{

ComboBox2.Items.Add(sd[0].Tostring());

}

sd.close();

class1.con.close();

 

 

答案:        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Visible = true;
            if (radioButton1.Checked == true && tabControl1.SelectedIndex == 0) //精确查找好友
            {
                panel1.Visible = false;
                if (yanzheng())
                {
                    button3.Visible = true;
                    button4.Visible = true;
                    pictureBox1.Visible = true;
                    if (txtId.Text.Trim() == "<请输入对方数字帐号>")
                    {
                        txtId.Text = "";
                    }
                    if (txtNickName.Text.Trim() == "<请输入对方昵称>")
                    {
                        txtNickName.Text = "";
                    }
                    string sql = "select id,nickname,sex,age,faceId,FriendshipPolicyId from users ";
                    if (txtId.Text.Trim() != "" && txtNickName.Text.Trim() != "")
                    {
                        sql = sql + string.Format("where id={0} and nickname like '{1}'", txtId.Text.Trim(),txtNickName.Text.Trim());
                    }
                    else if (txtId.Text.Trim() != "" && txtNickName.Text.Trim() == "")
                    {
                        sql = sql + string.Format("where id='{0}'",txtId.Text.Trim());
                    }
                    else if (txtId.Text.Trim() == "" && txtNickName.Text.Trim() != "")
                    {
                        sql = sql + string.Format("where nickname like '{0}'",txtNickName.Text.Trim());
                    }
                    ds = DBHelper.GetDataSet(sql, "user");
                    dataGridView1.DataSource = ds.Tables["user"];
                    if (ds.Tables["user"].Rows.Count < 1)
                    {
                        pictureBox2.Visible = true;
                    }
                }
                else
                {
                    dataGridView1.Visible = false;
                    txtId.Visible = true;
                    txtNickName.Visible = true;
                    SearchFriendFrom1 sh1 = new SearchFriendFrom1();
                    sh1.ShowDialog();
                }
            }
            if(radioButton2.Checked==true&&tabControl1.SelectedIndex==0)  //按条件查找好友
            {
                button3.Visible = true;
                button4.Visible = true;
                pictureBox1.Visible = true;
                panel1.Visible = true;
                string sql = "select id,nickname,sex,age,faceId,FriendshipPolicyId from users ";
                if (comboBox1.SelectedIndex == 0 && comboBox2.SelectedIndex == 0)
                {
                    sql = sql;
            

上一个:c#桌面提醒功能
下一个:c# 数据绑定 值成员

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,