C#链接数据库 C# 数据库 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 就是一个静态的属性,读取config配置文件的DbConfig节点的内容(从名称上面看应该返回一个数据库连接字符串)。 是的。config配置文件应该是这样的:<?xml version="1.0"?><configuration> <appSettings> ... <add key="DbConfig" value="User ID=xx;Password=xxx;Max Pool Size=512;Pooling=false;Data Source=xxx;"/> ... </appSettings></configuration>最终得到的值就是这个字符串:User ID=xx;Password=xxx;Max Pool Size=512;Pooling=false;Data Source=xxx; C#连接数据库//选择SQL Server所有数据库下拉列表中的数据库刷新按钮事件private void button4_Click(object sender, EventArgs e) { string str = "server=" + textBox6.Text + ";database=master;Uid=" + textBox5.Text + ";Pwd=" + textBox4.Text + ";"; comboBox1.DataSource = getTable(str); comboBox1.DisplayMember = "name"; comboBox1.ValueMember = "name"; }//获取数据库连接字符串private DataTable getTable(string str) { try { SqlConnection sqlcon = new SqlConnection(str); SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases ", sqlcon); DataTable dt = new DataTable("sysdatabases"); da.Fill(dt); return dt; } catch { return null; } }//进行数据库连接操作private void button3_Click(object sender, EventArgs e) { if (radioButton1.Checked == true) { if (textBox1.Text != "") { FileInfo FInfo = new FileInfo(textBox1.Text); string strExtention = FInfo.Extension; if (strExtention.ToLower() == ".mdb") { if (textBox2.Text != "") { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";UID=" + textBox2.Text + ";PWD=" + textBox3.Text + ";"; } else { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";"; } }//CodeGo.net/ else if (strExtention.ToLower() == ".xls") { strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended Properties=Excel 8.0;"; } } OleDbConnection oledbcon = new OleDbConnection(strCon); try { oledbcon.Open(); richTextBox1.Clear(); richTextBox1.Text = strCon + "\n连接成功……"; } catch { richTextBox1.Text = "连接失败"; } } else if (radioButton2.Checked == true) { if (checkBox1.Checked == true) { strCon = "Data Source=" + textBox6.Text + ";Initial Catalog =" + comboBox1.Text + ";Integrated Security=SSPI;"; } else if (checkBox2.Checked == true) { strCon = "Data Source=" + textBox6.Text + ";Database=" + comboBox1.Text + ";Uid=" + textBox5.Text + ";Pwd=" + textBox4.Text + ";"; } SqlConnection sqlcon = new SqlConnection(strCon); try { sqlcon.Open(); richTextBox1.Clear(); richTextBox1.Text = strCon + "\n连接成功……"; } catch { richTextBox1.Text = "连接失败"; } } } 读取配置文件(当然之前你还要有配置文件加载的操作,然后要确保配置文件的appsettings下有对应DbConfig节点):ConfigurationManager.AppSettings["DbConfig"].ToString(); 类的一个只读属性 返回连接串public static string Connstr 静态类如何扩展方法 Excel 读取日期格式,如何正确显示在窗体上 c#如何为foreach语句中的空值赋值? 如何才能学好c c#的一个简单问题,求指导 datagridview数据修改 问个问题.... 怎样解决用HttpWebRequest来获取超长网页的html代码 关于VS.NET2003的问题. 如何通过DefaultValue指定属性的枚举(enum)类型默认值, OleDbDataAdapter.fill 插入DataTable中并转为Excel 列错位了。 OPC客户端编程疑惑
config配置文件应该是这样的:<?xml version="1.0"?>
<configuration> <appSettings>
...
<add key="DbConfig" value="User ID=xx;Password=xxx;Max Pool Size=512;Pooling=false;Data Source=xxx;"/>
...
</appSettings></configuration>最终得到的值就是这个字符串:
User ID=xx;Password=xxx;Max Pool Size=512;Pooling=false;Data Source=xxx;
//选择SQL Server所有数据库下拉列表中的数据库刷新按钮事件
private void button4_Click(object sender, EventArgs e)
{
string str = "server=" + textBox6.Text + ";database=master;Uid=" + textBox5.Text + ";Pwd=" + textBox4.Text + ";";
comboBox1.DataSource = getTable(str);
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "name";
}
//获取数据库连接字符串
private DataTable getTable(string str)
{
try
{
SqlConnection sqlcon = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases ", sqlcon);
DataTable dt = new DataTable("sysdatabases");
da.Fill(dt);
return dt;
}
catch
{
return null;
}
}
//进行数据库连接操作
private void button3_Click(object sender, EventArgs e)
{
if (radioButton1.Checked == true)
{
if (textBox1.Text != "")
{
FileInfo FInfo = new FileInfo(textBox1.Text);
string strExtention = FInfo.Extension;
if (strExtention.ToLower() == ".mdb")
{
if (textBox2.Text != "")
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";UID=" + textBox2.Text + ";PWD=" + textBox3.Text + ";";
}
else
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";";
}
}//CodeGo.net/
else if (strExtention.ToLower() == ".xls")
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + textBox1.Text + ";Extended Properties=Excel 8.0;";
}
}
OleDbConnection oledbcon = new OleDbConnection(strCon);
try
{
oledbcon.Open();
richTextBox1.Clear();
richTextBox1.Text = strCon + "\n连接成功……";
}
catch
{
richTextBox1.Text = "连接失败";
}
}
else if (radioButton2.Checked == true)
{
if (checkBox1.Checked == true)
{
strCon = "Data Source=" + textBox6.Text + ";Initial Catalog =" + comboBox1.Text + ";Integrated Security=SSPI;";
}
else if (checkBox2.Checked == true)
{
strCon = "Data Source=" + textBox6.Text + ";Database=" + comboBox1.Text + ";Uid=" + textBox5.Text + ";Pwd=" + textBox4.Text + ";";
}
SqlConnection sqlcon = new SqlConnection(strCon);
try
{
sqlcon.Open();
richTextBox1.Clear();
richTextBox1.Text = strCon + "\n连接成功……";
}
catch
{
richTextBox1.Text = "连接失败";
}
}
}
读取配置文件(当然之前你还要有配置文件加载的操作,然后要确保配置文件的appsettings下有对应DbConfig节点):
ConfigurationManager.AppSettings["DbConfig"].ToString(); 类的一个只读属性 返回连接串
public static string Connstr