Need c# code optimized to insert CSV rows to MySql - c#

Here's what I'm trying to accomplish ... At the click of a button, I want to:
Copy existing data of the live "users" table to "old_users" table for backup purposes
Truncate the existing "new_users" table to prepare it to accept new rows
Parse through a CSV file to insert data into the "new_users" table
Truncate the existing "users" table and then copy data of the "new_users" table to "users" table
I am not a daily programmer and haven't created a program in a very long time. But I've piece-mealed some code together to get something that kinda works. Below is my current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
using Renci.SshNet;
using Renci.SshNet.Common;
using MySql.Data.MySqlClient;
namespace ValidationImport
{
public partial class ValiationImport : Form
{
public ValiationImport()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnImportToDatabase_Click(object sender, EventArgs e)
{
try
{
using (var client = new SshClient("1.1.1.1", "[username]", "[password]")) // establishing ssh connection to server where MySql is hosted
{
client.Connect();
if (client.IsConnected)
{
var portForwarded = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);
client.AddForwardedPort(portForwarded);
portForwarded.Start();
using (MySqlConnection con = new MySqlConnection("SERVER=localhost;PORT=3306;UID=[username];PASSWORD=[password];DATABASE=[dbname]")) // MySql database credentials
{
con.Open();
// Copying over the users table (with structure and indexes) to the old_users table. Truncating the new_users table to prepare for new data.
using (MySqlCommand cmd = new MySqlCommand("DROP TABLE test_old_users; CREATE TABLE test_old_users LIKE test_users; INSERT test_old_users SELECT * FROM test_users; TRUNCATE TABLE test_new_users;", con))
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
string fileName = "";
// Select the Validation file to import
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
fileName = dlg.FileName;
if (fileName != "")
{
using (TextFieldParser parser = new TextFieldParser(fileName))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters("|");
parser.ReadLine(); // Skip the first row of field title headers
while (!parser.EndOfData)
{
//Copy each row individually over to the MySql table
string[] row = parser.ReadFields();
using (MySqlCommand cmd = new MySqlCommand("INSERT INTO test_new_users (`Indv Id`, `Last Name`, `First Name`, `Middle Name`, `Birth date`, `Indv Addr Line 1`, `Indv Addr Line 2`, `Indv Addr Line 3`, `Indv City`, `Indv State`, `Indv Zip`, `Indv Country`, `Local Id`, `Local Name`, `Local User Id`, `Uniserv Id`, `Uniserv Name`, `Uniserv User Id`, `Chapter Name`, `Chapter User Id`, `Employer Id`, `Employer Name`, `Work Location Id`, `Work Location Name`, `Work Location User Id`, `Group Id`, `Group Name`, `Group Type Id`, `Group Type Name`, `SEA P01`, `Home Phone`, `Home Phone principal Ind`, `Home Phone Unlisted Ind`, `Mobile Phone`, `Mobile Phone Principal Ind`, `Mobile Phone Unlisted Ind`, `Home Email`, `Home Email Principal Ind`, `Work Email`, `Work Email Principal Ind`, `Other Email`, `Other Email Principal Ind`) VALUES (#IndvId, #LastName, #FirstName, #MiddleName, #BirthDate, #IndvAddrLine1, #IndvAddrLine2, #IndvAddrLine3, #IndvCity, #IndvState, #IndvZip, #IndvCountry, #LocalId, #LocalName, #LocalUserId, #UniservId, #UniservName, #UniservUserId, #ChapterName, #ChapterUserId, #EmployerId, #EmployerName, #WorkLocationId, #WorkLocationName, #WorkLocationUserId, #GroupId, #GroupName, #GroupTypeId, #GroupTypeName, #SEAP01, #HomePhone, #HomePhonePrincipalInd, #HomePhoneUnlistedInd, #MobilePhone, #MobilePhonePrincipalInd, #MobilePhoneUnlistedInd, #HomeEmail, #HomeEmailPrincipalInd, #WorkEmail, #WorkEmailPrincipalInd, #OtherEmail, #OtherEmailPrincipalInd);", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#IndvId", row[0]);
cmd.Parameters.AddWithValue("#LastName", row[1]);
cmd.Parameters.AddWithValue("#FirstName", row[2]);
cmd.Parameters.AddWithValue("#MiddleName", row[3]);
cmd.Parameters.AddWithValue("#BirthDate", row[4]);
cmd.Parameters.AddWithValue("#IndvAddrLine1", row[5]);
cmd.Parameters.AddWithValue("#IndvAddrLine2", row[6]);
cmd.Parameters.AddWithValue("#IndvAddrLine3", row[7]);
cmd.Parameters.AddWithValue("#IndvCity", row[8]);
cmd.Parameters.AddWithValue("#IndvState", row[9]);
cmd.Parameters.AddWithValue("#IndvZip", row[10]);
cmd.Parameters.AddWithValue("#IndvCountry", row[11]);
cmd.Parameters.AddWithValue("#LocalId", row[12]);
cmd.Parameters.AddWithValue("#LocalName", row[13]);
cmd.Parameters.AddWithValue("#LocalUserId", row[14]);
cmd.Parameters.AddWithValue("#UniservId", row[15]);
cmd.Parameters.AddWithValue("#UniservName", row[16]);
cmd.Parameters.AddWithValue("#UniservUserId", row[17]);
cmd.Parameters.AddWithValue("#ChapterName", row[18]);
cmd.Parameters.AddWithValue("#ChapterUserId", row[19]);
cmd.Parameters.AddWithValue("#EmployerId", row[20]);
cmd.Parameters.AddWithValue("#EmployerName", row[21]);
cmd.Parameters.AddWithValue("#WorkLocationId", row[22]);
cmd.Parameters.AddWithValue("#WorkLocationName", row[23]);
cmd.Parameters.AddWithValue("#WorkLocationUserId", row[24]);
cmd.Parameters.AddWithValue("#GroupId", row[25]);
cmd.Parameters.AddWithValue("#GroupName", row[26]);
cmd.Parameters.AddWithValue("#GroupTypeId", row[27]);
cmd.Parameters.AddWithValue("#GroupTypeName", row[28]);
cmd.Parameters.AddWithValue("#SEAP01", row[29]);
cmd.Parameters.AddWithValue("#HomePhone", row[30]);
cmd.Parameters.AddWithValue("#HomePhonePrincipalInd", row[31]);
cmd.Parameters.AddWithValue("#HomePhoneUnlistedInd", row[32]);
cmd.Parameters.AddWithValue("#MobilePhone", row[33]);
cmd.Parameters.AddWithValue("#MobilePhonePrincipalInd", row[34]);
cmd.Parameters.AddWithValue("#MobilePhoneUnlistedInd", row[35]);
cmd.Parameters.AddWithValue("#HomeEmail", row[36]);
cmd.Parameters.AddWithValue("#HomeEmailPrincipalInd", row[37]);
cmd.Parameters.AddWithValue("#WorkEmail", row[38]);
cmd.Parameters.AddWithValue("#WorkEmailPrincipalInd", row[39]);
cmd.Parameters.AddWithValue("#OtherEmail", row[40]);
cmd.Parameters.AddWithValue("#OtherEmailPrincipalInd", row[41]);
cmd.ExecuteNonQuery();
}
}
}
}
// Copying over the new_users table (with structure and indexes) to the users table.
using (MySqlCommand cmd = new MySqlCommand("DROP TABLE test_users; CREATE TABLE test_users LIKE test_new_users; INSERT test_users SELECT * FROM test_new_users;", con))
{
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
con.Close();
}
client.Disconnect();
}
else
{
Console.WriteLine("Client cannot be reached...");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
It's all "working" ... except I get an error when in debug mode in visual studio. Something about how it's taking too long to process. Oh, just so you know, there's around 38,000 rows in the CSV file that has to be inserted into the table.
If you have any suggestions or new code for me to try out, I'd greatly appreciate it! Thank you!

In many scenarios where you end up looping over data running the same queries repeatedly for different data, you can help speed things up by preparing the query once, before looping begins.
using (var cn = new MySqlConnection(...))
using (var cmd = cn.CreateCommand())
{
// Setup command
cmd.CommandText = "Some query";
var param1 = cmd.Parameters.Add("#paramName1", MySQLDbType.sometype);
var param2 = cmd.Parameters.Add("#paramName2", MySQLDbType.sometype);
cmd.Prepare();
// get data
// loop over data
foreach(var d in data)
{
param1.Value = d.somevalue;
param2.Value = d.anothervalue;
cmd.ExecuteNonQuery();
}
}
You can also address the parameters by index, like cmd.Parameters[0].Value.
Preparing the statement in this manner reduces the overhead of the work done to parse the query itself every iteration.
In one situation I had to deal with, I found the best solution was to combine the above with an INSERT query that used multiple values lists, of the form INSERT INTO X(a,b,c) VALUES (?,?,?),(?,?,?),(?,?,?). It made the actual implementation much more complicated (having to appropriately handle the "leftovers" at the end that would not fill all values lists), and relied on addressing the parameters by index; but did make a difference.
Edit: pseudo code to handle ssh connection closing on you
connect = true
Open datasource
while (data remains)
{
if (connect)
{
init ssh client
init mysql connection
init mysql command
connect = false
}
while (data remains and enough time remaining)
{
get next data
set command parameters
execute command
}
if (not enough time remaining)
{
close current connection and client
connect = true
}
}

Related

Basic inserting data into SQL Server database

I'm a newbie in Visual Studio and I want to make a database system that allows the user to insert, update, delete and search data using a Windows Forms application.
I already watched 3 tutorial how but I'm getting the same error. when I delete my ExecuteNonQuery() call, it doesn't have any error but the data I entered into my textboxes is not inserted into my database. When I put it back I'm getting this kind of error
ERROR:
CODE:
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.Data.SqlClient;
using System.Threading.Tasks;
namespace EaglePlannersDatabase
{
public partial class Form1 : Form
{
SqlConnection connection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Adrian\Documents\EaglePlannersDataBase.mdf;Integrated Security=True;Connect Timeout=30");
public Form1()
{
InitializeComponent();
}
private void InsertButton_Click(object sender, EventArgs e)
{
connection.Open();
SqlCommand cmd = new SqlCommand("Insert Into EAGLEPLANNERS(policy number,plan type,renewal date,name,age,address,birthday,email,home/office number,mode of payment,amount) values (#policy number,#plan type,#renewal date,#name,#age,#address,#birthday,#email,#home/office number,#mode of payment,#amount)", connection);
cmd.Parameters.AddWithValue("#policy number", int.Parse(policyNumbertxtbox.Text));
cmd.Parameters.AddWithValue("#plan type", planTypetxtbox.Text);
cmd.Parameters.AddWithValue("#renewal date", int.Parse(renewalDatetxtbox.Text));
cmd.Parameters.AddWithValue("#name", nametxtbox.Text);
cmd.Parameters.AddWithValue("#age", int.Parse(agetxtbox.Text));
cmd.Parameters.AddWithValue("#address", addresstxtbox.Text);
cmd.Parameters.AddWithValue("#birthday", int.Parse(birthdaytxtbox.Text));
cmd.Parameters.AddWithValue("#email", (emailtxtbox.Text));
cmd.Parameters.AddWithValue("#home/office number", int.Parse(homeofficetxtbox.Text));
cmd.Parameters.AddWithValue("#mode of payment", (modeofpaymenttxtbox.Text));
cmd.Parameters.AddWithValue("#amount", int.Parse(amounttxtbox.Text));
cmd.ExecuteNonQuery();
connection.Close();
policyNumbertxtbox.Text = "";
planTypetxtbox.Text = "";
renewalDatetxtbox.Text = "";
nametxtbox.Text = "";
agetxtbox.Text = "";
addresstxtbox.Text = "";
birthdaytxtbox.Text = "";
emailtxtbox.Text = "";
homeofficetxtbox.Text = "";
modeofpaymenttxtbox.Text = "";
amounttxtbox.Text = "";
MessageBox.Show("Record inserted successfully!");
}
}
}
As a newbie, getting the pieces working first, then applying to the user interface I would apply second. I will try to summarize each piece. First your connection itself looked strange as others have pointed out. I would try to first make sure the connection itself works before applying any attempt at sql insert/update/delete going on. So you might try
SqlConnection connection = new SqlConnection(
#"Data Source (LocalDB)\MSSQLLocalDB; AttachDbFilename=C:\Users\Adrian\Documents\EaglePlannersDataBase.mdf;
Integrated Security=True;
Connect Timeout=30" );
private void TestConnect()
{
if( connection.Open() )
// great, you have a good connection
connection.Close();
else
// message to yourself why a failed connection and fix it...
}
Once you know your connection is good, then on to your sql-insert. Having good column names is important. Dont try to be fancy with human readable with spaces types of column names, just causes headaches. Use simple and direct as others have pointed out in prior comments. Also, when parameterizing, I have tried to always slightly alter the insert/update/delete parameters with a "p" prefix indicating the PARAMETER FOR the column, such as
insert into SomeTable ( oneColumn, secondCol ) values ( #pOneColumn, #pSecondCol )
just to avoid bad confusion. If an error comes out via "oneColumn" vs "pOneColumn" in the message, you KNOW which thing is at fault. The column itself does not work, or the specific parameter/value being supplied.
Next, readability of your SQL statements, especially as they get longer. Use spaces and I typically use a leading "#" before the quoted sql command to allow for line continuations as I edited your previous answer. So the same above insert would be written more like
var cmd = new SqlCommand(
#"insert into SomeTable
( oneColumn,
secondCol
)
values
( #pOneColumn,
#pSecondCol
)", connection );
So if you ever needed to add additional columns (or remove), you can see the paired set of insert columns vs parameters much easier.
Now the testing. Don't try to work off some user-entered values, put in known VALID values so you dont have to worry about user entered values. Get the command to WORK, then pull values from interface later. (continuing from above sample)
cmd.Parameters.AddWithValue("#pOneColumn", 17 );
cmd.Parameters.AddWithValue("#pSecondCol", "some Text");
Then try to execute that and make sure IT works. Once it does, THEN start pulling from your user interface
cmd.Parameters.AddWithValue("#pOneColumn", int.Parse( yourTextControl.Text ));
cmd.Parameters.AddWithValue("#pSecondCol", anotherTextControl.Text );
Find your mistakes BEFORE you let any human interaction get into and screw-up the rest of what you think SHOULD work.
Note, if you make public properties to your view models such as
public int someAge {get; set;}
and then set the bindings of the data entry control in the screen to this someAge property, it will only allow a numeric entry to be entered and will otherwise have a value of zero if someone tries to put in text. Similarly if you are dealing with dates, and if date/time, always use datetime fields for querying purposes vs formatted date as a text field. You will thank yourself in the future when querying for things within date range periods. HTH
Finally,try to avoid using AddWithValue. Instead, properly identify the expected data type as described in the linked article. I just left original context to your code for testing and debug researching purposes.
This would be the possible answer on your question, what I have done here, first I changed your connection string by removing AttachDbFilename attribute, and replace that by adding of Initial Catalog attribute where I set the name of your database.
Next thing, I declared variables where the values from textboxes will be stored, these values of variables will be our parameters, this is not necessary to do, it is just my own style, you can keep as you done.
I have seen also, that you are not using try/catch/finally block and you are closing the connection in the same part of code where you opening the connection, and maybe that is the reason why your values are not being stored into the table, so I decided to round your code within that block. If you don't know what is try/catch/finally block you can read the documentation here . We are opening the connection and do all operations on database in try block, in catch block we are catching all errors that might be caused in our application and in finally we are closing the connection. You will also notice that I created additional check, where I checking the result of ExecuteNonQuery() method, and if result of ExecuteNonQuery method is equals to 1 - records is inserted successfully, otherwise it fails.
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.Data.SqlClient;
using System.Threading.Tasks;
namespace EaglePlannersDatabase
{
public partial class Form1 : Form
{
SqlConnection connection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;Initial Catalog=EaglePlannersDataBase;Integrated Security=True;Connect Timeout=30");
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void InsertButton_Click(object sender, EventArgs e)
{
int policyNumber = Convert.ToInt32(policyNumbertxtbox.Text);
string planType = planTypetxtbox.Text;
int renewalDate = Convert.ToInt32(renewalDatetxtbox.Text);
string name = nametxtbox.Text;
int age = Convert.ToInt32(agetxtbox.Text);
string address = addresstxtbox.Text;
int birthday = Convert.ToInt32(birthdaytxtbox.Text);
string email = emailtxtbox.Text;
int homeOfficeNumber = Convert.ToInt32(homeofficetxtbox.Text);
string modeOfPayment = modeofpaymenttxtbox.Text;
int amount = Convert.ToInt32(amounttxtbox.Text);
try
{
connection.Open();
SqlCommand cmd = new SqlCommand("Insert Into tbl1 (PolicyNumber,planType,renewalDate,name,age,address,birthday,email,homeOfficeNumber,modeOfPayment,amount) values (#PolicyNumber,#planType,#renewalDate,#name,#age,#address,#birthday,#email,#homeOfficeNumber,#modeOfPayment,#amount)", connection);
cmd.Parameters.AddWithValue("#PolicyNumber",policyNumber);
cmd.Parameters.AddWithValue("#planType",planType);
cmd.Parameters.AddWithValue("#renewalDate",renewalDate);
cmd.Parameters.AddWithValue("#name",name);
cmd.Parameters.AddWithValue("#age",age);
cmd.Parameters.AddWithValue("#address",address);
cmd.Parameters.AddWithValue("#birthday",birthday);
cmd.Parameters.AddWithValue("#email",email);
cmd.Parameters.AddWithValue("#homeOfficeNumber",homeOfficeNumber);
cmd.Parameters.AddWithValue("#modeOfPayment",modeOfPayment);
cmd.Parameters.AddWithValue("#amount",amount);
int result = cmd.ExecuteNonQuery();
if(result == 1)
{
MessageBox.Show("Record Inserted Successfully!");
policyNumbertxtbox.Text = "";
planTypetxtbox.Text = "";
renewalDatetxtbox.Text = "";
nametxtbox.Text = "";
agetxtbox.Text = "";
addresstxtbox.Text = "";
birthdaytxtbox.Text = "";
emailtxtbox.Text = "";
homeofficetxtbox.Text = "";
modeofpaymenttxtbox.Text = "";
amounttxtbox.Text = "";
}
else
{
MessageBox.Show("Something went wrong!");
}
}
catch(SqlException ex)
{
MessageBox.Show("We have found error with operation on database: " + ex.Message);
}
catch(Exception ex)
{
MessageBox.Show("We have found error in your code: " + ex.Message);
}
finally
{
connection.Close();
}
}
}
}
here's my new code
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.Data.SqlClient;
using System.Threading.Tasks;
namespace EaglePlannersDatabase
{
public partial class Form1 : Form
{
SqlConnection connection = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Adrian\Documents\EaglePlannersDataBase.mdf;Integrated Security=True;Connect Timeout=30");
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void InsertButton_Click(object sender, EventArgs e)
{
connection.Open();
SqlCommand cmd = new SqlCommand(
#"Insert Into tbl1
( PolicyNumber,
planType,
renewalDate,
name,
age,
address,
birthday,
email,
homeOfficeNumber,
modeOfPayment,
amount )
values
( #PolicyNumber,
#planType,
#renewalDate,
#name,
#age,
#address,
#birthday,
#email,
#homeOfficeNumber,
#modeOfPayment,
#amount )", connection);
cmd.Parameters.AddWithValue("#PolicyNumber", int.Parse(policyNumbertxtbox.Text));
cmd.Parameters.AddWithValue("#planType", planTypetxtbox.Text);
cmd.Parameters.AddWithValue("#renewalDate", int.Parse(renewalDatetxtbox.Text));
cmd.Parameters.AddWithValue("#name", nametxtbox.Text);
cmd.Parameters.AddWithValue("#age", int.Parse(agetxtbox.Text));
cmd.Parameters.AddWithValue("#address", addresstxtbox.Text);
cmd.Parameters.AddWithValue("#birthday", int.Parse(birthdaytxtbox.Text));
cmd.Parameters.AddWithValue("#email", (emailtxtbox.Text));
cmd.Parameters.AddWithValue("#homeOfficeNumber", int.Parse(homeofficetxtbox.Text));
cmd.Parameters.AddWithValue("#modeOfPayment", (modeofpaymenttxtbox.Text));
cmd.Parameters.AddWithValue("#amount", int.Parse(amounttxtbox.Text));
cmd.ExecuteNonQuery();
connection.Close();
policyNumbertxtbox.Text = "";
planTypetxtbox.Text = "";
renewalDatetxtbox.Text = "";
nametxtbox.Text = "";
agetxtbox.Text = "";
addresstxtbox.Text = "";
birthdaytxtbox.Text = "";
emailtxtbox.Text = "";
homeofficetxtbox.Text = "";
modeofpaymenttxtbox.Text = "";
amounttxtbox.Text = "";
MessageBox.Show("Record Inserted Successfully!");
}
}
}
here's my database

SQLite database not saving data Once the Windows Form app stops

I have been attempting to use SQLite database for a Winform C# Project.
The .db file is saved in my project directory and my code has access to it fine. If I enter the value, it works, if I enter the same it errors, as it should.
Once I stop the Windows Form Program and Start it again, all the data I entered is cleared.
Question - How can I ensure the data in the .db file is saved. I guess there's a setting I'm missing.
using (SQLiteConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
cnn.Open();
using (SQLiteTransaction transaction = cnn.BeginTransaction())
{
using (SQLiteCommand cmd = new SQLiteCommand(cnn))
{
SQLiteParameter IDParam = new SQLiteParameter();
SQLiteParameter NameParam = new SQLiteParameter();
cmd.CommandText = "INSERT into Person (ID, Name) VALUES (#ID, #Name)";
cmd.Parameters.AddWithValue("ID", 1);
cmd.Parameters.AddWithValue("Name", "Lewis");
i = cmd.ExecuteNonQuery();
}
transaction.Commit();
//cnn.Execute("insert into Person (ID, Name) values (1, 'Lewis')", person)
}
}
Code for reference.
Resolved
Add the Sqlite database via the tool.

How to read a CSV file into an SQL table

I'm trying to read a CSV file into a table that I have created in Visual Studio. I want to validate the values in the file to see if they are correct, if all the values pass the checks, it will go into the table. If any values are not correct, an error report will be created using a JSON file.
I have already got some test data ready but I'm not sure how to separate the correct data from the incorrect data after the checker are complete.
public partial class NHSBatchChecker : Form
{
public NHSBatchChecker()
{
InitializeComponent();
}
public void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = #"C:\Users\wy6282\Desktop\VS\NHSBATCHCHECKER\Test.txt"; // Start in C: drive
openFileDialog1.Title = "Browse Text Files";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.DefaultExt = "txt"; // Extension of file is txt only
openFileDialog1.Filter = "Text|*.txt||*.*"; //Only text files allowed
openFileDialog1.CheckFileExists = true; // Error message if file does not exist
openFileDialog1.CheckPathExists = true; // Error message if invalid file path
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string connectionstring;
SqlConnection cnn;
connectionstring = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\wy6282\Desktop\VS\NHSBATCHCHECKER\nhsBatchChecker\Results.mdf;Integrated Security=True";
cnn = new SqlConnection(connectionstring);
cnn.Open();
SqlCommand command;
SqlDataAdapter adaper = new SqlDataAdapter();
string sql = "";
sql = "Insert into Results(NHS Number, Date of Birth, First Name, Last Name, Title, Gender) values()";
command = new SqlCommand(sql, cnn);
adaper.InsertCommand = new SqlCommand(sql, cnn);
adaper.InsertCommand.ExecuteNonQuery();
command.Dispose();
cnn.Close();
How do I add my valid records into the sql table?
you're trying to do too much in one go.
rule number 1: always split your problem into manageable junks:
read data from CSV.
filter incorrect data
save filtered data to database.
Now you have 3 distinct pieces of work to focus on.
Reading data from a CSV is trivial, there are many libraries that can help with that. Do a bit of research and pick one.
Create a class which holds the properties you need for validation checks and also those you want saved in the database.
Your goal is to create a list of these objects, one per row in csv. Of course, you may not be able to read everything in one go depending on how much data your csv holds, but you can pick a library which can deal with whatever size you have.
Now you have a list of objects. Write an algorithm which determines what is valid and what not, based on the rules you need. Your goal here is to end up with a possibly smaller list of the same objects, chucking out the invalid ones.
Save whatever is left in the database.
You need to start thinking about how you organize you code, don't just throw everything into the Click event of a button. You can create a model class to hold your objects, maybe create a separate class library where you can put your csv reading method.
Another class library perhaps for your filtering algorithm(s).
Your Click event should be fairly slim and only call library methods when it needs to do something. This is separation of concerns or SOC, which is a Solid Principle.
I am not sure how you plan to validate the datapoints, but the code below shows how to pull data from a CSV and load it into a SQL Server table.
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.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Configuration;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string server = "EXCEL-PC\\EXCELDEVELOPER";
string database = "AdventureWorksLT2012";
string SQLServerConnectionString = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI", server, database);
string CSVpath = #"C:\Users\Ryan\Documents\Visual Studio 2010\Projects\Bulk Copy from CSV to SQL Server Table\WindowsFormsApplication1\bin"; // CSV file Path
string CSVFileConnectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};;Extended Properties=\"text;HDR=Yes;FMT=Delimited\";", CSVpath);
var AllFiles = new DirectoryInfo(CSVpath).GetFiles("*.CSV");
string File_Name = string.Empty;
foreach (var file in AllFiles)
{
try
{
DataTable dt = new DataTable();
using (OleDbConnection con = new OleDbConnection(CSVFileConnectionString))
{
con.Open();
var csvQuery = string.Format("select * from [{0}]", file.Name);
using (OleDbDataAdapter da = new OleDbDataAdapter(csvQuery, con))
{
da.Fill(dt);
}
}
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(SQLServerConnectionString))
{
bulkCopy.ColumnMappings.Add(0, "MyGroup");
bulkCopy.ColumnMappings.Add(1, "ID");
bulkCopy.ColumnMappings.Add(2, "Name");
bulkCopy.ColumnMappings.Add(3, "Address");
bulkCopy.ColumnMappings.Add(4, "Country");
bulkCopy.DestinationTableName = "AllEmployees";
bulkCopy.BatchSize = 0;
bulkCopy.WriteToServer(dt);
bulkCopy.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
}

Store A List Of Data In Mysql DataBase?

i have a list of String Data i would Like to Store in my MySQL database Table in Column "Categories".
Is there a way to store it at a go because its a long list.`
public Class PickerView{
List<string> CategoriesPicker = new List<string>();
public Button SaveItemsButton = new Button();
public PickerView()
{
CategoriesPicker.Items.Add("Hotels & Travel");
CategoriesPicker.Items.Add("Restaurant");
CategoriesPicker.Items.Add("Wholesalers");
CategoriesPicker.Items.Add("Automotives");
CategoriesPicker.Items.Add("Pets");
CategoriesPicker.Items.Add("Musical Instruments Services");
CategoriesPicker.Items.Add("Specialty Food");
CategoriesPicker.Items.Add("Food");
CategoriesPicker.Items.Add("Boutique");
CategoriesPicker.Items.Add("Home & Gardens");
CategoriesPicker.Items.Add("Shopping");
CategoriesPicker.Items.Add("Education");
CategoriesPicker.Items.Add("Books,Mags,Music & Video");
CategoriesPicker.Items.Add("Fashion");
CategoriesPicker.Items.Add("Event Planning & Services");
CategoriesPicker.Items.Add("Arts & Craft");
CategoriesPicker.Items.Add("Local Services");
CategoriesPicker.Items.Add("NightLife(Bars..)");
SaveItemsButton.Clicked += SavedItemsButton_Clicked
}
private void SavedItemsButton_Clicked(object sender, System.EventArgs e)
{
string sqlstring = "server=; port= ; user id =;Password= ;Database=test;";
MySqlConnection conn = new MySqlConnection(sqlstring);
try
{
conn.Open();
}
catch (MySqlException ex)
{
throw ex;
}
string Query = "INSERT INTO test.maintable (Categories)values('" +(**//I DONT KNOW WHAT TO WRITE HERE TO SAVE ALL AT ONCE**) + "');";
MySqlCommand cmd = new MySqlCommand(Query, conn);
cmd.ExecuteReader();
conn.Close();
}
}`
How do i save the list of items in CategoriesPicker in database when SaveItemsButton is clicked.
Simple use the mysql insert into statement.
insert into tbl1 values
(1, 'Name1', 1, null),
(2, 'Name2', 2, null),
(3, 'Name3', 1, null);
(3, 'Name3', 1, null) is of course the structure of tbl1
This will work in any language you use or even in comand line
MySQL (and MariaDB) don't have list-valued column types since that goes against first normal form in database design.
What you do instead is what #nbk suggested and, using whatever database framework you chose, insert multiple rows. Here's a sample table definition for MariaDB/MySQL if you don't have one already:
Microsoft's current recommended way of interacting with a database is Entity Framework Core. The documentation there can help you with connecting to a database, creating a table, adding rows to a table and saving all of that to the database.
Hope that helps!
I added using blocks to your code to ensure that your database objects are closed and disposed even if there is an error.
Set the query text only once, it stays the same for each iteration of the loop. Likewise the parameter is added outside the loop. Only the value of the parameter is changed inside the loop. We use .ExecuteNonQuery (not reader) for Insert, Update or Delete. .ExecuteReader is used for returning data.
private List<string> CategoriesPicker = new List<string>();
//add all the items to the list
private void SavedItemsButton_Clicked(object sender, System.EventArgs e)
{
string sqlstring = "server=; port= ; user id =;Password= ;Database=test;";
using (MySqlConnection conn = new MySqlConnection(sqlstring))
{
string Query = "INSERT INTO test.maintable (Categories)values(#Category);";
using (MySqlCommand cmd = new MySqlCommand(Query, conn))
{
cmd.Parameters.Add("#Category", MySqlDbType.VarChar);
try
{
conn.Open();
}
catch (MySqlException ex)
{
throw ex;
}
foreach (String item in CategoriesPicker)
{
cmd.Parameters["#Category"].Value = item;
cmd.ExecuteNonQuery();
}
}
}
}

Store values in sql db as .txt file

I am developing a simple domain application using web services.I got the domain information using whois wsdl.That is working well but I am getting the entire data the problem is I need only selected data from that server like domain name, creation date,expire date.
In my design I made two text box when entered any domain name in textbox1 if it is exist in whois server it must show selective information into textbox2.
I tried to store these values in a text file and save its name in db but its not working for me. Any ideas friends
Here is my coding
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class do01 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string st = TextBox1.Text;
wservices.whois myservices = new wservices.whois();
TextBox2.Text = myservices.GetWhoIS(st);
}
}
Here is my screenshot
If you have any ideas just shoot it here friends.... :)
NOTE: See update below for a change of answer
I'm assuming that what you want is to have a table with this information as the plain text in a column.
A basic table definition
CREATE TABLE WhoIsData
(
Id INT PRIMARY KEY IDENTITY(1,1),
WhoIsData NVARCHAR(MAX)
)
I don't know what st is (which is the data you send to the service, it may be useful in your table if you also want to store that too).
C# code (roughly - I've not tested it)
string st = TextBox1.Text;
wservices.whois myservices = new wservices.whois();
string textData = myservices.GetWhoIS(st);
TextBox2.Text = textData;
using(SqlConnection conn = new SqlConnection(connectionString));
{
SqlCommand cmd = new SqlCommand("INSERT INTO WhoIsData(WhoIsData) VALUES(#text);");
cmd.Connection = conn;
cmd.Parameters.AddWithValue("#text", textData);
conn.Open();
cmd.ExecuteNonQuery();
}
I'd separate out the SQL Calls from your user interface classes in real life. But for the sake of the example I've put it all together. Also, I've not renamed your text boxes, although TextBox1 and TextBox2 are really bad names.
UPDATE
Okay - You want to store the content in a text file, but I'm assuming you want to store a link to the text file in the database (you've not been very specific)
The Table definition:
CREATE TABLE WhoIsData
(
Id INT PRIMARY KEY IDENTITY(1,1),
DomainName NVARCHAR(256),
WhoIsFile NVARCHAR(256)
)
The C# code:
string st = TextBox1.Text;
wservices.whois myservices = new wservices.whois();
string textData = myservices.GetWhoIS(st);
TextBox2.Text = textData;
string fileName = Guid.NewGuid().ToString();
using(var file = File.OpenWrite(fileName))
{
file.Write(textData);
}
using(SqlConnection conn = new SqlConnection(connectionString));
{
SqlCommand cmd = new SqlCommand("INSERT INTO WhoIsData(DomainName, WhoIsFile) VALUES(#domainName, #fileName);");
cmd.Connection = conn;
cmd.Parameters.AddWithValue("#fileName", fileName);
cmd.Parameters.AddWithValue("#domainName", domainName);
conn.Open();
cmd.ExecuteNonQuery();
}
Again, I've not tested this, but it is roughly what you want to do. Also, you need to provide a mechanism for generating the file name, I've just used a guid as it pretty much guarantees uniqueness.

Categories

Resources