连接模式下的编程代码 [英] programming code in connected mode

查看:54
本文介绍了连接模式下的编程代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击名为"show"的按钮时,我希望反手显示的雇员表显示在数据网格视图中.我需要使用C#.net
中的数据读取器对象,SQL连接,SQL命令在连接模式下对此进行编程的代码
我想在不使用数据集对象的情况下运行此代码(仅适用于sql连接,sql命令和数据读取器对象)....请帮助我……

When I click on button called "show" , I want that my employee table that is in backhand displayed in data grid view. I need programming code for this in the connected mode using data reader object, sql connection, sql command ...in C#.net

I want to run this code without using data set object (only with sql connection, sql command and data reader object)....please help me out......

推荐答案

Try:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM PurchaseTypes", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                }
            }
        }
    }

这为您提供了基础知识-现在您可以将其自己输出到DataGridView.

下次,尝试自己尝试一下!这可能意味着您需要阅读,进行一些研究或进行一些学习-但这就是您要学习的课程,不是吗?

That gives you the basics - now you can output it to the DataGridView yourself.

And next time, try actually trying it yourself! This may mean some reading, a bit a research, maybe some learning - but that is what you are on a course for, isn''t it?


在按钮"click"事件上显示":-
sql connection con =新的sql connection(连接字符串");
con.open();
sql命令com =新的sql命令(从emp中选择*"),con);
sqldatareader dr = com.execute reader();
数据集ds =新的数据集();
datatable dt =新的datatable(表");
ds.tables.add(dt);
ds.load(dr,loadoption.preserve更改,ds.tables [0]);
datagridview.datasource = ds.tables [0];
On the click event of button "Show":-
sql connection con= new sql connection("connection string");
con.open();
sql command com= new sql command ("select *from emp"), con);
sqldatareader dr= com.execute reader();
dataset ds= new dataset();
datatable dt= new datatable ("table");
ds.tables.add(dt);
ds.load(dr, loadoption.preserve changes, ds.tables[0]);
datagridview.datasource= ds.tables[0];


con = new SqlConnection("Data Source = HP-PC \\ SQLEXPRESS; Initial Catalog =" + comboBox1.SelectedItem +;集成安全性= True);
cmd =新的SqlCommand(",con);
cmd.CommandText =从* + comboBox2.SelectedItem +"中选择*;
DataTable dt = new DataTable();
con.Open();
SqlDataReader dr2 = cmd.ExecuteReader();
dt.Clear();
dt.Load(dr2);
dataGridView1.DataSource = dt;
con.Close();
con = new SqlConnection("Data Source=HP-PC\\SQLEXPRESS;Initial Catalog="+comboBox1.SelectedItem+";Integrated Security=True");
cmd = new SqlCommand("", con);
cmd.CommandText = "select * from "+comboBox2.SelectedItem+"";
DataTable dt = new DataTable();
con.Open();
SqlDataReader dr2 = cmd.ExecuteReader();
dt.Clear();
dt.Load(dr2);
dataGridView1.DataSource = dt;
con.Close();


这篇关于连接模式下的编程代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆