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

再发一贴

   我现在有个数据库 叫 crmManager  

下面有表

Autocar
Categories
Manufacturer



我想吧这些表在winform程序里查出来,然后显示在combox 中  、
连接字符串
private static string connstring = "Data Source=.;Initial Catalog=crmManager";Integrated Security=True";

sql语句该怎么写

我希望回帖的朋友可以自己先试一下再发好么 --------------------编程问答--------------------
引用楼主 yonghumingcunzaiwdl 的回复:
  

我希望回帖的朋友可以自己先试一下再发好么

--------------------编程问答--------------------
select * from  crmManager.用户名.SYSOBJECTS  where xtype='u' 


你的语句写成这样应该能行的,怎么不行了? --------------------编程问答-------------------- Initial Catalog=crmManager
其实你默认连接的就是那个数据库,应该不会出错了 --------------------编程问答-------------------- 估计你的问题解决不了了 我这里确实没问题

create database crmManager 
go
create table Autocar(id int)
go
create table Categories(id varchar(10))
go
create table Manufacturer(id varchar(10))
go 

select * from  SYSOBJECTS  where type='u'


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("Server=localhost\\sql2005;database=crmManager;uid=sa;pwd=");
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from SYSOBJECTS  where type='u'", conn);
                //我试了下 可以的 不知道你的怎么不可以 
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(reader["name"]);
                }
            }
            catch(Exception e){
                Console.WriteLine(e.Message);    
            }
            finally 
            {
                conn.Close();                
            }
        }
    }
}

E:\>csc Test.cs
Microsoft (R) Visual C# 2005 编译器 版本 8.00.50727.1433
用于 Microsoft (R) Windows (R) 2005 Framework 版本 2.0.50727
版权所有 (C) Microsoft Corporation 2001-2005。保留所有权利。


E:\>Test
Autocar
Categories
Manufacturer

E:\>

--------------------编程问答--------------------
引用 4 楼 bancxc 的回复:
估计你的问题解决不了了 我这里确实没问题
SQL codecreatedatabase crmManagergocreatetable Autocar(idint)gocreatetable Categories(idvarchar(10))gocreatetable Manufacturer(idvarchar(10))goselect*from  SYSOBJECTSwhere type='u'
C# codeusing System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;namespace ConsoleApplication2
{class Program
    {staticvoid Main(string[] args)
        {
            SqlConnection conn=new SqlConnection("Server=localhost\\sql2005;database=crmManager;uid=sa;pwd=");try
            {
                conn.Open();
                SqlCommand cmd=new SqlCommand("select * from SYSOBJECTS  where type='u'", conn);//我试了下 可以的 不知道你的怎么不可以                SqlDataReader reader= cmd.ExecuteReader();while (reader.Read())
                {
                    Console.WriteLine(reader["name"]);
                }
            }catch(Exception e){
                Console.WriteLine(e.Message);    
            }finally 
            {
                conn.Close();                
            }
        }
    }
}

E:\>csc Test.cs
Microsoft (R) Visual C#2005 编译器 版本8.00.50727.1433
用于 Microsoft (R) Windows (R)2005 Framework 版本2.0.50727
版权所有 (C) Microsoft Corporation2001-2005。保留所有权利。


E:\>Test
Autocar
Categories
Manufacturer

E:\>

同意,
呵呵,你先读取看看先,不绑定到COMBOBOX --------------------编程问答--------------------

连接字符串中要指定你的数据库名
[code=C#]
string strcon = "server=.;uid=sa;pwd=;database=你的数据库名";
SqlConnection sqlcon = new SqlConnection(strcon);
string cmdtxt = "select [name],[id] from sysobjects where xtype='u' and status>0 order by name";
SqlDataAdapter sqlda = new SqlDataAdapter(cmdtxt, sqlcon);
DataTable dt = new DataTable();
sqlda.Fill(dt);

数据表绑定COMBOBOX --------------------编程问答-------------------- 6楼的补充
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "name";
//comboBox1.ValueMember = "name";
--------------------编程问答-------------------- DataSet ds =new DataSer();
using(SqlConnection con = new SqlConnection(""))
{
string sql= "select * from  dbo.SYSOBJECTS  where xtype='u'";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
da.Fill(ds);
}
comboBox1.DataSource = ds; 
comboBox1.DisplayMember = ""; 
comboBox1.ValueMember = ""; 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,