首页上一页 1 下一页尾页 3 条记录 1/1页
把数据库连接写入ini文件提示成功,但内容没变,4个textbox为空时,写入ini文件出错
发表在C#图书答疑
2010-06-30
是否精华
是
否
版块置顶:
是
否
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
{
//设计器自动生成的
public Form1()
{
InitializeComponent();
}
//定义ini文件路径
private string FFileName = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\") + 1) + "dataCon.ini";
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();
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string FFileName);
private void button1_Click(object sender, EventArgs e)
{
{
string DSource = ReadString("Server", "Name", "");
string DBase = ReadString("DataBase", "Name", "");
string DUid = ReadString("userid", "Name", "");
string DPwd = ReadString("userpass", "Name", "");
SqlConnection sqlcon = new SqlConnection("Server=" + DSource + ";User Id=" + DUid + ";Pwd=" + DPwd + ";DataBase=" + DBase + "");
sqlcon.Open();
MessageBox.Show("数据库连接成功");
}
}
private void button2_Click(object sender, EventArgs e)
{
{
StringBuilder temp = new StringBuilder(200);
foreach (object ct in Controls)
{
if (ct.GetType().ToString() == "System.Windows.Forms.TextBox")
{
TextBox tx = (TextBox)ct;
if (tx.Text == "")
{
MessageBox.Show(tx.Tag.ToString() + "不能为空");
}
}
}
string Server = textBox1.Text;//INI文件中的段落
string DataBase = textBox2.Text;//INI文件中的关键字
string userid = textBox3.Text;//INI文件中的关键字
string userpass = textBox4.Text;//INI文件中的关键字
int i = GetPrivateProfileString(Server, DataBase, "无法读取对应数值!", temp, 200, FFileName);//判断是否有记录
if (temp.ToString() == "无法读取对应数值!")
{
WritePrivateProfileString(Server, DataBase, userid, userpass);
MessageBox.Show("成功写入INI文件!", "信息");
}
else
{
MessageBox.Show("此信息已写入");
}
}
}
}
}
以下是ini文件
[Server]
name=.
[DataBase]
name=pubs
[userid]
name=sa
[userpass]
name=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
{
//设计器自动生成的
public Form1()
{
InitializeComponent();
}
//定义ini文件路径
private string FFileName = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\") + 1) + "dataCon.ini";
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();
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string FFileName);
private void button1_Click(object sender, EventArgs e)
{
{
string DSource = ReadString("Server", "Name", "");
string DBase = ReadString("DataBase", "Name", "");
string DUid = ReadString("userid", "Name", "");
string DPwd = ReadString("userpass", "Name", "");
SqlConnection sqlcon = new SqlConnection("Server=" + DSource + ";User Id=" + DUid + ";Pwd=" + DPwd + ";DataBase=" + DBase + "");
sqlcon.Open();
MessageBox.Show("数据库连接成功");
}
}
private void button2_Click(object sender, EventArgs e)
{
{
StringBuilder temp = new StringBuilder(200);
foreach (object ct in Controls)
{
if (ct.GetType().ToString() == "System.Windows.Forms.TextBox")
{
TextBox tx = (TextBox)ct;
if (tx.Text == "")
{
MessageBox.Show(tx.Tag.ToString() + "不能为空");
}
}
}
string Server = textBox1.Text;//INI文件中的段落
string DataBase = textBox2.Text;//INI文件中的关键字
string userid = textBox3.Text;//INI文件中的关键字
string userpass = textBox4.Text;//INI文件中的关键字
int i = GetPrivateProfileString(Server, DataBase, "无法读取对应数值!", temp, 200, FFileName);//判断是否有记录
if (temp.ToString() == "无法读取对应数值!")
{
WritePrivateProfileString(Server, DataBase, userid, userpass);
MessageBox.Show("成功写入INI文件!", "信息");
}
else
{
MessageBox.Show("此信息已写入");
}
}
}
}
}
以下是ini文件
[Server]
name=.
[DataBase]
name=pubs
[userid]
name=sa
[userpass]
name=000000