已有56人关注
把数据库连接写入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 
分享到:
精彩评论 3
小科_mrkj
学分:43 LV2
2010-06-30
沙发
读者朋友:
    您好,您的INI文件有问题,INI文件格式如下:
[Server] 
name=. 
name=pubs 
name=sa 
name=000000 
关于如何对INI文件进行读写,你可以参考《C#范例完全自学手册》中的实例073。
wufayou
学分:0 LV1
TA的每日心情
第五天
2023-03-18 21:24:17
2010-06-30
板凳
应该没问题呀,我的程序能读取该ini文件,但写不到ini文件中
小科_mrkj
学分:43 LV2
2010-06-30
地板
读者朋友:
    您好,你写入的代码写的不对,应该以下面这种格式进行写入:
WritePrivateProfileString("Server","name",Server,FFileName);
首页上一页 1 下一页尾页 3 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照