What I am trying to do, is allow the user to set the server ip, port, database and username in an .ini file.
Then I Am trying to take that info and import it into my c# code. I am new to c# so please be gentle .... Here is my code thus far
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 MySql.Data.MySqlClient;
namespace Pxi_Gm_Tool
{
public partial class addAccounts : Form
{
public addAccounts()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void addAccounts_Load(object sender, EventArgs e)
{
MySqlConnectionStringBuilder connBuilder =
new MySqlConnectionStringBuilder();
connBuilder.Add("Database", "pxi");
connBuilder.Add("Data Source", "localhost");
connBuilder.Add("User Id", "root");
connBuilder.Add("Password", "");
MySqlConnection connection =
new MySqlConnection(connBuilder.ConnectionString);
MySqlCommand cmd = connection.CreateCommand();
MySqlCommand command = connection.CreateCommand();
connection.Open();
try
{
command.CommandText = "INSERT INTO accounts (aident, pass, privs,status,created) VALUES (textbox1.text, textbox2.text, 5, 0, CURRENT_TIMESTAMP)";
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Username in use");
}
}
}
}
Loading

Vijaya KadiyalaRank #1,085 · 1.8K reputationPosted Mar 16, 2009, 8:52 AM EDT
Hi,
Instead of storign this information in .ini file!! why can't you store this in web.config file!! why are you giving an option to the use to enter IP,Database etc information?? is this dynamic??
Thanks -- Vijaya Kadiyala
http://dotnetvj.blogspot.com
Joshua Sparks5 reputationPosted Mar 16, 2009, 9:05 AM EDT
Yes it is dynamic, I want the user to beable to connect to what sql server they wish to. I.E they have my tool on thier pc but the database server on another I want them to be able connect to that database server.
I have never heard of using a web.config file ... as I said I am new to programming.
Vijaya KadiyalaRank #1,085 · 1.8K reputationPosted Mar 16, 2009, 9:35 AM EDT
In your requirement you can't use web.config. By looking at the code you have problem in writing/reading the data from a .ini file..
Check out the below link which will help you to read and write.
http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx
Once you are done with this then we can start querying the data.
Thanks
-- Vijaya Kadiyalahttp
://dotnetvj.blogspot.comJoshua Sparks5 reputationPosted Mar 16, 2009, 11:34 AM EDT
How do I add the variables so they are replaced by the lines in the ini file ?
Jan MontanoRank #1,295 · 1.3K reputationPosted Mar 16, 2009, 6:42 PM EDT
Here's an article regarding how to read/write xml ini files. Good luck!
Moses Spencer20 reputationPosted Mar 19, 2015, 1:17 AM EDT
For example if you have something like the following INI content:
server ip = localhost
database = pxi
username = root
password =
Then you would use the following:
MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder(); IniFile ini = new IniFile(); ini.Load("user.ini"); IniSection userSection = ini.Sections[0]; connBuilder.Add("Database", userSection.Keys["database"].Value); connBuilder.Add("Data Source", userSection.Keys["server ip"].Value); connBuilder.Add("User Id", userSection.Keys["username"].Value); connBuilder.Add("Password", userSection.Keys["password"].Value);