The Visual Studio.NET about C#

 

C#关于ComboBox控件链接数据库的应用操作

string lianjieshujuku ="Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Data Source=数据库名称";

OleDbConnectioncon=new OleDbConnection(lianjieshujuku);

con.Open();

DataSet ds =new DataSet();

OleDbDataAdapter da= new OleDbDataAdapter("select stuId from tb_student",con);

Da.Fill(ds);

comboBox1.DataSource=ds.Tables[0].DefaultView;

comboBox1.DisplayMember = "stuId";

注意:需要引用using System.Data.OleDb;命名空间                                 

C#关于ComboBox控件查询功能的应用操作

comboBox1.Items.Clear();

comboBox1.Items.Add("你好");

comboBox1.Items.Add("大海");

comboBox1.Items.Add("天空");

comboBox1.Items.Add("中国");

comboBox1.Items.Add("小三");

comboBox1.Items.Add("小四");

comboBox1.Items.Add("本世纪");

comboBox1.Items.Add("功臣");

this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

C#关于ListBox控件连接数据库并添加选项功能应用操作

private System.Windows.Forms.ListBox lbSocure;

private System.Windows.Forms.ListBox lbChoose;

SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=数据库名称");

con.Open();

SqlCommand com = new SqlCommand("select * from 表名", con);

SqlDataReader dr = com.ExecuteReader();

this.Items.Clear();

while (dr.Read())

{

this.lbSocure.Items.Add(dr[1].ToString());

}

private void button2_Click(object sender, EventArgs e)//全部添加到选择的项中

{

for (int i = 0; i < lbSocure.Items.Count; i++)

{

lbSocure.SelectedIndex=i;

lbChoose.Items.Add(lbSocure.SelectedItem.ToString());

}

lbSocure.Items.Clear();

}

private void button3_Click(object sender, EventArgs e)//全部添加到数据源中

{

for (int i = 0; i < lbChoose.Items.Count; i++)

{

lbChoose.SelectedIndex = i;

lbSocure.Items.Add(lbChoose.SelectedItem.ToString());

}

lbChoose.Items.Clear();

}

注意:需要引用using System.Data. SqlClient;命名空间                               

C#关于ListBox控件连接数据库的功能应用操作

SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=数据库名称");

con.Open();

SqlCommand com = new SqlCommand("select * from 表名", con);

SqlDataReader dr = com.ExecuteReader();

while (dr.Read())

{

this.listBox1.Items.Add(dr[1].ToString());

}

注意:需要引用using System.Data. SqlClient;命名空间                               

C#关于ListBox控件借助绑定实现数据选择录入功能应用

private void Form1_Load(object sender, EventArgs e)

{

SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=;database=数据库名称");

con.Open();

SqlCommand com = new SqlCommand("select * from 表名", con);

SqlDataReader dr = com.ExecuteReader();

while (dr.Read())

{

this.listBox1.Items.Add(dr[1].ToString());

}

}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)

{

if (listBox1.SelectedItem.ToString() != null)

{

textBox2.Text = listBox1.SelectedItem.ToString();

}

}

private void button2_Click(object sender, EventArgs e)

{

textBox1.Text = "";

textBox2.Text = "";

}

private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)

{

if (listBox1.SelectedItem.ToString() != null)

{

textBox2.Text = listBox1.SelectedItem.ToString();

}

}

注意:需要引用using System.Data. SqlClient;命名空间                               

C#关于ListBox控件拒绝加入重复信息的功能应用

if (textBox1.Text == "")

{

button3.Text = "商品名称不能为空";

button3.ForeColor = Color.Red;

textBox1.Focus();

}

else

{

if (listBox1.Items.Count > 0)

{

for (int i = 0; i < listBox1.Items.Count; i++)

{

if (listBox1.Items[i].ToString() == textBox1.Text)

{

button3.Text = "商品名称已存在";

button3.ForeColor = Color.Red;

textBox1.Text = "";

textBox1.Focus();

return;

}

}

listBox1.Items.Add(textBox1.Text);

textBox1.Text = "";

return;

}

listBox1.Items.Add(textBox1.Text);

textBox1.Text = "";

}

C#关于ListBox控件利用选择控件实现复杂查询功能应用

public void GetScoure( string strSql)

{

SqlConnection con = new SqlConnection("server=(local);pwd=;uid=sa;database=数据库名称");

con.Open();

SqlCommand com = new SqlCommand(strSql, con);

dataGridView1.Rows.Clear();

SqlDataReader dr = com.ExecuteReader();

while (dr.Read())

{

dataGridView1.Rows.Add(dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString());

}

dr.Close();

con.Close(); 

}

private void bntEsce_Click(object sender, EventArgs e)

{

ckClass.Checked = false;

ckName.Checked = false;

ckId.Checked = false;

rdbMan.Checked = false;

rdbWoMan.Checked = false;

}

private void ckId_CheckedChanged(object sender, EventArgs e)

{

if (ckId.Checked == true)

{

txtstuId.Enabled = true;

txtstuId.Focus();

}

else

     {

            txtstuId.Text = "";

            txtstuId.Enabled = false;

     }

}

private void ckClass_CheckedChanged(object sender, EventArgs e)

{

if (ckClass.Checked == true)

{

txtClasss.Enabled = true;

txtClasss.Focus();

}

else

{

txtClasss.Text = "";

txtClasss.Enabled = false;

}

}

private void ckName_CheckedChanged(object sender, EventArgs e)

{

if (ckName.Checked == true)

{

txtName.Enabled = true;

txtName.Focus();

}

else

{

txtName.Text = "";

txtName.Enabled = false;

}

}

public string strSql;//用于存储SQL语句

public string strId;//用于存学生编号

public string strName;//用于存储学生姓名

public string strClass;//用于存储学生班级

public string strSex;//用于存储学生姓别

public static int intCount=0;//控制数据组索引

public string[] strScoure = new string[4];

public int intAdd ;//用来判断SQL语句数组

private void bntSearch_Click(object sender, EventArgs e)

{

intCount = 0;

if (txtstuId.Text != "")

     {

strId = "学生编号  like '%" + txtstuId.Text + "%'"  ;

strScoure[intCount]=strId;

intCount++;

}

     if (txtName.Text != "")

{

strName = "学生姓名 like '%" + txtName.Text + "%' ";

strScoure[intCount]= strName;

intCount++;

}

     if (txtClasss.Text != "")

     {

strClass = "所在年级 like '%" + txtClasss.Text + "%'";

strScoure[intCount]= strClass;

intCount++;

     }

     if (rdbMan.Checked == true)

     {

             strSex = "学生性别 like '%" + rdbMan.Text + "%'";

            strScoure[intCount]= strSex;

            intCount++;

     }

     if (rdbWoMan.Checked == true)

     {

strSex = "学生性别 like '%" + rdbWoMan.Text + "%'";

           strScoure[intCount]= strSex;

             intCount++;

     }

     for (int i = 0; i < strScoure.Length; i++)

     {

     if (strScoure[i] != null)

     {

strSql += strScoure[i];

intAdd++;

}

}// end

switch (intAdd)

{

case 0:

strSql = "select * from tb_01";

break;

case 1:

strSql = "select * from tb_01 where "+ strScoure[0];

break;

     case 2:

     strSql = "select * from tb_01 where " + strScoure[0] + " and " + strScoure[1];

break;

case 3:

strSql = "select * from tb_01 where " + strScoure[0] + " and " + strScoure[1] + " and " + strScoure[2];

break;  

case 4:

strSql = "select * from tb_01 where " + strScoure[0] + " and " + strScoure[1] + " and " + strScoure[2] + " and " + strScoure[3];

break;

}

GetScoure(strSql);

intAdd = 0;

intCount = 0;

strSql = "";

for (int i = 0; i < strScoure.Length; i++)

{

if (strScoure[i] != null)

{

strScoure[i] = null;

}

}// end

}

public void getScoure(string str_name)

{

SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=;database=数据库名称");

con.Open();

SqlCommand com = new SqlCommand("select * from 表名 where 列名= '" + 输入控件名称 + "'", con);

SqlDataReader dr = com.ExecuteReader();//

while (dr.Read())

{

txtId.Text = dr[0].ToString();

txtName.Text = dr[1].ToString();

txtEmpty.Text = dr[2].ToString();

txtSex.Text = dr[3].ToString();

txtAge.Text = dr[4].ToString();

txtXueLi.Text = dr[5].ToString();

txtTeching.Text = dr[6].ToString();

}

dr.Close();

            con.Close();

        }

        public string str_name;//变量,得单当选择的值

        private void listViewEmpty_Click(object sender, EventArgs e)

        {

            str_name = listViewEmpty.SelectedItems[0].SubItems[0].Text;//当前选择的值

            if (str_name != null)

            {

                getScoure(str_name);

            }

        }

C#关于ListView控件连接数据库功能应用

#region 数据库连接字符串

//string findatabase = Application.StartupPath + "//数据库名称";

string lianjieshujuku = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=密码;Data Source=数据库名称";

#endregion

#region 连接数据库

OleDbConnection con = new OleDbConnection(lianjieshujuku);

OleDbCommand cmd = new OleDbCommand("Select * from 表名", con);

con.Open();

OleDbDataReader read = cmd.ExecuteReader();

while (read.Read())

{

ListViewItem item = new ListViewItem(read[0].ToString());

item.SubItems.Add(read[1].ToString());

this.listView1.Items.Add(item);

}

con.Close();

con.Dispose();

}

#endregion

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值