SQLite Database with C# executing very slowly - c#

I have my sqlite database stored in C:\program-name\db\db.s3db.
I have a table members with about 70 records. So now there is a procedure that i have to do, which is copying all account numbers from the members table to the other table act_monthly_listings.
Its showing that it's executing but it's excrutiatingly slow. I don't know if it is because of my code. Here is a snippet:
Connection:
private void SetConnection()
{
sql_con = new SQLiteConnection
("Data Source=C:\\credit-union\\db\\cu.s3db;Version=3;New=False;Compress=True;");
}
On ButtonClick Code:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "Working Please Wait";
string init_month = dtInit.Value.Month.ToString();
string init_yr = dtInit.Value.Month.ToString();
SetConnection();
sql_con.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = "select ec_no from members";
cmd.Connection = sql_con;
DR = cmd.ExecuteReader();
int count = 0;
while (DR.Read())
{
DR.Read();
this.Text = "Account : " + DR.GetInt32(0).ToString();
string SQL = "INSERT INTO act_monthly_listings (year,month,act) VALUES ("+ init_yr + "," + init_month + "," + DR.GetInt32(0).ToString() + ")";
//Clipboard.SetText(SQL)
if (ExecuteQuery(SQL))
{
count++;
}
}
if(count > 0){
MessageBox.Show("Accounts Initialized!", "Success", MessageBoxButtons.OK,MessageBoxIcon.Information);
}else{
MessageBox.Show("Initialization Failed!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
sql_con.Close();
}

That is because of while you are executing insert query equal to row count which make your program run very slow so this is better to use
INSERT INTO-SELECT like:
string init_month = dtInit.Value.Month.ToString();
string init_yr = dtInit.Value.Month.ToString();
SQLiteCommand cmd = new SQLiteCommand();
cmd.CommandText = "INSERT INTO act_monthly_listings (year,month,act) select #y,#m,ec_no from members";
cmd.Parameters.Add(new SQLiteParameter("#y", init_yr ));
cmd.Parameters.Add(new SQLiteParameter("#m", init_month ));
cmd.Connection = sql_con;
cmd.ExecuteNonQuery();
all the rows inserted in one shoot, while you are using while until reader.Read() pointer arrived to last row the DataReader is connecting with DB (Need to alive connection) that may make connection pooling slow plus you are executing a query per Read avoid this,
suppose you have a million record then what happened by your code?

Related

How to count number of records set under a condition?

I'm basically new in coding, and I'm in uni, we have a small project to complete and I'm using OleDb to do it, as I have used it before, I'm trying to count the number of rows where a field is equal to a specific value, but I always get 0. To be more clear, I want statistics showing percentage of males/females who completed the survey(the program I'm making), currently I have 1 male and 2 females, so my string for all rows shows 3, which is correct, but when I want to count all the rows that contain females, it shows 0, so the total percentage is 0, and the textbox that's supposed to show the stat shows 0, here is the code:
private void Form2_Load(object sender, EventArgs e)
{
String connectionString;
connectionString = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Users\kubas\Desktop\MyProject\ProjectDB.accdb'";
using (OleDbConnection myConnection = new OleDbConnection(connectionString))
{
string Gender1 = "Female";
OleDbConnection con;
OleDbCommand cmd = new OleDbCommand();
OleDbCommand cmd1 = new OleDbCommand();
con = new OleDbConnection(connectionString);
con.Open();
cmd.CommandText = "SELECT Count(Gender) FROM Survey WHERE Gender = '" + Gender1 + "'";
cmd1.CommandText = "SELECT Count(Gender) FROM Survey";
cmd.Connection = con;
cmd1.Connection = con;
con.Close();
try
{
con.Open();
int total = (Int32)cmd.ExecuteScalar();
int total1 = (Int32)cmd1.ExecuteScalar();
int tot;
tot = total / total1 * 100;
string ttal = Convert.ToString(tot);
textBox1.Text = ttal;
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
sorry if there is something you can't understand, English isn't my first language.
Maybe try this command query: "SELECT Count(Gender) FROM Survey WHERE Gender = \"" + Gender1 + "\"";
From what I can remember. access uses quotation marks for strings.

Insert text file data into SQL Server database table of specific column

I have attached my code in which I tried to read a .txt file with many records.
The same text file data I need to insert into the SQL Server database table in specific columns. Here is the code I wrote by taking reference from some where.
protected void BtnUpload_Click(object sender, EventArgs e)
{
FileUpload(x);
}
private void FileUpload(List<string> x)
{
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
string fileName = Path.Combine(#"C:\Users\user\Desktop\SBS", FileUpload1.FileName);
if(FileUpload1.HasFile)
{
try
{
con.Open();
List<string> x;
for (int i = 0; i <= x.Count - 9; i += 9)
{
SqlCommand myCommand = new SqlCommand("INSERT INTO SBSFile (SBSBranchCode, BranchName, FinYear, BrChallanNo, TransDate, MajorHead, ReceiptPayment, Amount, PlanNonPlan) " +
string.Format("Values('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}')", x[i], x[i + 1], x[i + 2], x[i + 3], x[i + 4], x[i + 5], x[i + 6], x[i + 7], x[i + 8], x[i + 9]), con);
myCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
}
}
}
example of my file :
1 |abc| |bcd| |101|
here | denoted separator of column and yes every line should be inserted in the table where in specific column is available
Can anyone help me to insert file data into the SQL Server table?
Please help me to solve the issue
I wouldn't use a program at all... I'd use the "Import" feature in SQL server to import a pipe-delimited file. For example How to import pipe delimited text file data to SQLServer table
Here is an example of working code that will read data from a Text file, where that data is delimited by |. Multiple insert statements will be executed within a single transaction, in case of failure using an All Or Nothing principle.
[TestMethod]
public void TestInsertDataFromFile()
{
String fileName = #"D:\SampleData.txt";
String connectionString = #"Server=MyTestDBServer; Database=TestingDatabase; Trusted_Connection=True;";
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlTransaction transaction = conn.BeginTransaction())
{
String insertCommand = #"INSERT INTO SBSFile (SBSBranchCode, BranchName, FinYear, BrChallanNo, TransDate, MajorHead, ReceiptPayment, Amount, PlanNonPlan) ";
insertCommand += #"VALUES (#sbsBranchCode, #branchName, #finYear, #brChallanNo, #transDate, #majorHead, #receiptPayment, #amount, #planNonPlan)";
String[] fileContent = File.ReadAllLines(fileName);
using (SqlCommand command = conn.CreateCommand())
{
command.CommandText = insertCommand;
command.CommandType = CommandType.Text;
command.Transaction = transaction;
foreach (String dataLine in fileContent)
{
String[] columns = dataLine.Split('|');
command.Parameters.Clear();
command.Parameters.Add("sbsBranchCode", SqlDbType.VarChar).Value = columns[0];
command.Parameters.Add("branchName", SqlDbType.VarChar).Value = columns[1];
command.Parameters.Add("finYear", SqlDbType.VarChar).Value = columns[2];
command.Parameters.Add("brChallanNo", SqlDbType.VarChar).Value = columns[3];
command.Parameters.Add("transDate", SqlDbType.VarChar).Value = columns[4];
command.Parameters.Add("majorHead", SqlDbType.VarChar).Value = columns[5];
command.Parameters.Add("receiptPayment", SqlDbType.VarChar).Value = columns[6];
command.Parameters.Add("amount", SqlDbType.VarChar).Value = columns[7];
command.Parameters.Add("planNonPlan", SqlDbType.VarChar).Value = columns[8];
command.ExecuteNonQuery();
}
}
transaction.Commit();
}
}
}
Important things to note
NEVER have an empty catch {} handler as you will never know if there are problems.
When talking to the database using externally specified values, ALWAYS use parameters to prevent SQL Injection attacks.
Make use of Transactions if you are doing multiple inserts from a single source. It will make recovery possible without having to unpick the data manually.
Where possible (when classes implement IDisposable) use a using(...) block to ensure resources are released and not blocking/locking.
use this code:
SqlConnection con = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
string fileName = Path.Combine(#"C:\Users\user\Desktop\SBS", FileUpload1.FileName);
if (FileUpload1.HasFile)
{
var lines = File.ReadAllLines(fileName);
try
{
con.Open();
foreach (var line in lines)
{
var columns = line.Split('|');
SqlCommand myCommand = new SqlCommand("INSERT INTO SBSFile (SBSBranchCode, BranchName, FinYear, BrChallanNo, TransDate, MajorHead, ReceiptPayment, Amount, PlanNonPlan) " +
$"Values('{columns[0]}', '{columns[1]}','{columns[2]}','{columns[3]}','{columns[4]}','{columns[5]}','{columns[6]}','{columns[7]}','{columns[8]}''{columns[9]}')");
myCommand.ExecuteNonQuery();
}
con.Close();
}
catch (Exception ex)
{
}
}

Saving Data from a gridview into a local database in asp.net

I am currently writing a piece of code where the user is supposed to insert a few information about an employee and press one button populate for populating a gridview and another one to save the information in gridview into a local database. While running the what I wrote so far there is a consistent error saying "SqlExeption was unhandled by the user code. I have been trying to fix it but without success. It complains on conn.Open();
This is that specific piece of code:
protected void SaveButton_Click(object sender, EventArgs e)
{
string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(#"Data Source = C:\EmployeeWebProject\EmployeeWebProject\App_Data\EmployeeDatabase.sdf"))
{
using (SqlCommand comm = new SqlCommand("SELECT * FROM Employee"))
{
comm.Connection = conn;
conn.Open();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
StrQuery = #"INSERT INTO Employee VALUES ("
+ GridView1.Rows[i].Cells[0].ToString() + ", "
+ GridView1.Rows[i].Cells[1].ToString() + ", "
+ GridView1.Rows[i].Cells[2].ToString() + ", "
+ GridView1.Rows[i].Cells[3].ToString() + ", "
+ GridView1.Rows[i].Cells[4].ToString() + ");";
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}
}
finally
{
}
}
To avoid SQL injection and use properly parametrized queries, and also use the SQL Server CE connection and command objects, try this code:
protected void SaveButton_Click(object sender, EventArgs e)
{
string StrQuery;
try
{
// define connection string and INSERT query WITH PARAMETERS
string connectionString = #"Data Source = C:\EmployeeWebProject\EmployeeWebProject\App_Data\EmployeeDatabase.sdf";
string insertQry = "INSERT INTO Employees(Col1, Col2, Col3, Col4, Col5) " +
"VALUES(#Col1, #Col2, #Col3, #Col4, #Col5);";
// define connection and command for SQL Server CE
using (SqlCeConnection conn = new SqlCeConnection(connectionString))
using (SqlCeCommand cmd = new SqlCeCommand(insertQry, conn))
{
// add parameters to your command - adapt those *as needed* - we don't know your table structure,
// nor what datatype (and possibly length) those parameters are !
cmd.Parameters.Add("#Col1", SqlDbType.Int);
cmd.Parameters.Add("#Col2", SqlDbType.VarChar, 100);
cmd.Parameters.Add("#Col3", SqlDbType.VarChar, 100);
cmd.Parameters.Add("#Col4", SqlDbType.VarChar, 100);
cmd.Parameters.Add("#Col5", SqlDbType.VarChar, 100);
conn.Open();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
// set parameter values
cmd.Parameters["#Col1"].Value = Convert.ToInt32(GridView1.Rows[i].Cells[0]);
cmd.Parameters["#Col2"].Value = GridView1.Rows[i].Cells[1].ToString();
cmd.Parameters["#Col3"].Value = GridView1.Rows[i].Cells[1].ToString();
cmd.Parameters["#Col4"].Value = GridView1.Rows[i].Cells[1].ToString();
cmd.Parameters["#Col5"].Value = GridView1.Rows[i].Cells[1].ToString();
cmd.ExecuteNonQuery();
}
}
}
finally
{
}
}

Data type mismatch in criteria expression(Convert.ToInt32(cmd.ExecuteScalar());)

I am trying to Display a name in the textbox from the database if the ID entered by the user matches the record in the MS ACCESS DATABASE.
I'm getting the error Data type mismatch in criteria expression at the line int count = Convert.ToInt32(cmd.ExecuteScalar());
The following is my aspx.cs code-
protected void Button1_Click(object sender, EventArgs e)
{
clear();
idcheck();
DataTable dt = new DataTable();
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\dfg\fd\Visual Studio 2010\WebSites\WebSite21\App_Data\UPHealth.mdb");
con.Open();
str = "SELECT [DoctorName] FROM [DoctorInfo] WHERE DoctorID='" + TextBox1.Text.Trim() + "'";
OleDbCommand cmd = new OleDbCommand(str, con);
OleDbDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox2.Text = dr["DoctorID"].ToString();
dr.Close();
con.Close();
}
}
public void idcheck()
{
OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\dfg\fd\Visual Studio 2010\WebSites\WebSite21\App_Data\UPHealth.mdb");
con.Open();
str = "SELECT count(DoctorName) FROM [DoctorInfo] WHERE DoctorID='" + TextBox1.Text.Trim() + "'";
OleDbCommand cmd = new OleDbCommand(str, con);
int count = Convert.ToInt32(cmd.ExecuteScalar());
if (count > 0)
{
Label21.Text = "Doctor Name";
}
else
{
Label21.Text = "Id Does not Exist";
}
}
void clear()
{
TextBox2.Text = "";
}
I guess that is because you as passing in an ID, which is usually a numeric value, as a text field:
DoctorID='" + TextBox1.Text.Trim() + "'
Which should be:
DoctorID=" + TextBox1.Text.Trim()
Another problem arises, since you are vulnerable to SQL injection. What if the text box contained 1; delete users? Then your entire users table would be empty. The lesson learned: use parameterized queries!
Then you can express the SQL as:
DoctorID= ?
And add the parameter to the request:
cmd.Parameters.AddWithValue("?", TextBox1.Text.Trim());

Inserting selected data using checkbox from gridview

When I select the in the gridview using checkbox, I want it to insert the data into the database, but it is not adding it. My code is below, please see where I am going wrong.
public partial class HomeTeamCheckList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LiveGameReporting Window
SubmitLineUp.Attributes.Add("onclick", "PassValues();");
SubmitLineUp.Text = "Submit " + Session["HomeTeam"] + "'s Line Up";
}
protected void SubmitLineUp_Click(object sender, EventArgs e)
{
String GameID = string.Empty;
String Name = string.Empty;
String Number = string.Empty;
int GKGVCount = GoalKeeperGridView.Rows.Count;
foreach (GridViewRow gkrow in GoalKeeperGridView.Rows)
{
GameID = (String)Session["GameID"];
Number = gkrow.Cells[0].Text;
Name = gkrow.Cells[1].Text;
SqlConnection connection = new SqlConnection(("Data Source=ROBEL-HP;Initial Catalog=RocoSportsDB;Integrated Security=True"));
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = #"INSERT INTO HTLineUp (GameID, HomeTeamLineUpNo, HomeTeamLineUpName) VALUES (#GameID,#Number,#Name)";
cmd.Parameters.AddWithValue("#GameID", GameID);
cmd.Parameters.AddWithValue("#Number", Number);
cmd.Parameters.AddWithValue("#Name", Name);
cmd.ExecuteNonQuery();
}
}
}
}
Two thoughts:
Use a try-catch to see if you're getting any SQL errors.
Check the return value of the cmd.ExecuteNonQuery(); to see if any rows were actually affected / inserted.
Like this:
SqlConnection connection = new SqlConnection(("Data Source=ROBEL-HP;Initial Catalog=RocoSportsDB;Integrated Security=True"));
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = #"INSERT INTO HTLineUp (GameID, HomeTeamLineUpNo, HomeTeamLineUpName) VALUES (#GameID,#Number,#Name)";
cmd.Parameters.AddWithValue("#GameID", GameID);
cmd.Parameters.AddWithValue("#Number", Number);
cmd.Parameters.AddWithValue("#Name", Name);
// use a debugger to see if any rows were actually affected / inserted
int rowsAffected = cmd.ExecuteNonQuery();
}
catch(SQLException error)
{
// Use a debugger to see if you are getting an error on execution
string errorText = error.message;
}
Your query string looks ok, so it could be a permissions error. But the steps above will help you track it down.

Categories

Resources