首页上一页 1 下一页尾页 3 条记录 1/1页
读取不到ini文件中的数据库连接,请老师指导一下,谢谢。
发表在C#图书答疑
2010-06-29
是否精华
是
否
版块置顶:
是
否
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
namespace 从ini文件中读取数据库连接
{
public partial class Form1 : Form
{
private string FFileName = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\") + 1) + "dataCon.ini";//[font color=#FF0000]此ini文件我放在\bin\Debug目录下
[/font]
public Form1()
{
InitializeComponent();
}
public Form1(string filename)
{
FFileName = filename;
}
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName);
public string ReadString(string section, string key, string def)
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, temp, 1024, FFileName);
return temp.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
{
string DSource = ReadString("Connection", "Server", "");
string DBase = ReadString("Connection", "database", "");
string DUid = ReadString("Connection", "userid", "");
string DPwd = ReadString("Connection", "userpass", "");
//SqlConnection sqlcon = new SqlConnection("Data Source=" + DSource + ";Database=" + DBase + ";Uid=" + DUid + ";Pwd=" + DPwd + ";");
//SqlConnection sqlcon = new SqlConnection("Server=(local);User Id=sa;Pwd=000000;DataBase=pubs");[font color=#FF0000]用这句可以连接成功
[/font] SqlConnection sqlcon = new SqlConnection("Server=" + DSource + ";User Id=" + DUid + ";Pwd=" + DPwd + ";DataBase=" + DBase + "");//[font color=#FF0000]用这句就连接不到[/font]
sqlcon.Open();
MessageBox.Show("数据库连接成功");
}
}
}
}
以下是ini文件内容:
[Server]
server=(local)
[DataBase]
database=pubs
[userid]
userid=sa
[userpass]
userpass=000000
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
namespace 从ini文件中读取数据库连接
{
public partial class Form1 : Form
{
private string FFileName = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\") + 1) + "dataCon.ini";//[font color=#FF0000]此ini文件我放在\bin\Debug目录下
[/font]
public Form1()
{
InitializeComponent();
}
public Form1(string filename)
{
FFileName = filename;
}
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
int nSize,
string lpFileName);
public string ReadString(string section, string key, string def)
{
StringBuilder temp = new StringBuilder(1024);
GetPrivateProfileString(section, key, def, temp, 1024, FFileName);
return temp.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
{
string DSource = ReadString("Connection", "Server", "");
string DBase = ReadString("Connection", "database", "");
string DUid = ReadString("Connection", "userid", "");
string DPwd = ReadString("Connection", "userpass", "");
//SqlConnection sqlcon = new SqlConnection("Data Source=" + DSource + ";Database=" + DBase + ";Uid=" + DUid + ";Pwd=" + DPwd + ";");
//SqlConnection sqlcon = new SqlConnection("Server=(local);User Id=sa;Pwd=000000;DataBase=pubs");[font color=#FF0000]用这句可以连接成功
[/font] SqlConnection sqlcon = new SqlConnection("Server=" + DSource + ";User Id=" + DUid + ";Pwd=" + DPwd + ";DataBase=" + DBase + "");//[font color=#FF0000]用这句就连接不到[/font]
sqlcon.Open();
MessageBox.Show("数据库连接成功");
}
}
}
}
以下是ini文件内容:
[Server]
server=(local)
[DataBase]
database=pubs
[userid]
userid=sa
[userpass]
userpass=000000