首先在vs2005中引入using System.Data.SqlClient命名空间
/// 查询
/// </summary>
/// <returns></returns>
public DataTable Select()
{
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESSInitial Catalog=TestIntegrated Security=True")//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open()
string sql = "select * from users"
SqlCommand cmd = new SqlCommand(sql, conn)
SqlDataAdapter sda = new SqlDataAdapter(cmd)
DataTable dt = new DataTable()
sda.Fill(dt)
可以用一个SqlDataReader 将数据库的信息加入到ComboBox控件中:SqlDataReader read=new SqlDataReader("select id from user")
if(read.hasrows)
{
while(read.Read())
commobox.items.add(read["id"].tostring())
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)