|
楼主 |
发表于 2013-4-3 09:45:35
|
显示全部楼层
[code=csharp]
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
namespace Fondo_Manage
{
class Conn
{
private string IP;
private string DK;
private string DbName;
private string DbUser;
private string Pwd;
/// <summary>
/// 链接数据库设置
/// </summary>
/// <param name="ip">IP地址</param>
/// <param name="dbDK">端口地址</param>
/// <param name="dbName">数据库</param>
/// <param name="dbUser">用户名字</param>
/// <param name="key">用户密码</param>
public Conn(string ip, string dbDK, string dbName, string dbUser, string key)
{
IP = ip;
DK = dbDK;
DbName = dbName;
DbUser = dbUser;
Pwd = key;
}
#region 对数据库服务器操作
public SqlConnection ServerConn()
{
string Douhao = ",";
if (DK == "") { DK = "1433"; }
string ServerDK = IP + Douhao + DK;
SqlConnection thisConnection = new SqlConnection(@"Server=" + ServerDK + ";user id=" + DbUser + ";password=" + Pwd+";Trusted_Connection=no");
thisConnection.Open();
return thisConnection;
}
#endregion
}
}[/code]
[code=csharp]
private void comboBox3_Click(object sender, EventArgs e)
{
string db = "";
string strsql = this.fondosql.Text.Trim(), strdk = this.fondoduankou.Text.Trim(), strid = this.fondosa.Text.Trim(), strpwd = this.fondopwd.Text.Trim();
Conn conn = new Conn(strsql, strdk, db, strid, strpwd);
SqlConnection sqlConnection = conn.ServerConn();
SqlCommand cmd = new SqlCommand("select * from sysdatabases", sqlConnection);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
comboBox3.Items.Clear();
while (dr.Read())
{
comboBox3.Items.Add(dr[0].ToString());//循环读区数据
}
}
}[/code]
效果图
|
|