C# - unable to connect to a local ACCDB file with new SqlConnection() - c#

Doing a university project that requires an application that stores and outputs data from a local access DB file.
So far I have very little C# knowledge but I have a basic understanding of how to write code and what functions and methods are.
I have looked up a lot of different questions on StackOverflow and other sites about this kind of issue, yet I could not find a solution to my problem.
Here is the code -
private void search_db(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
string search_query = search_input.Text;
conn.ConnectionString = "Data Source=D:/Projects/WIP/8515/Academy/Travel Agency C#/Hotel_Agency/Hotel_Database.accdb;";
conn.Open();
SqlCommand command = new SqlCommand("SELECT * FROM Customers WHERE (name LIKE #query) OR (EGN LIKE #query)", conn);
command.Parameters.Add(new SqlParameter("query", search_query));
// Create new SqlDataReader object and read data from the command.
using (SqlDataReader reader = command.ExecuteReader())
{
// while there is another record present
while (reader.Read())
{
// write the data on to the screen
Console.WriteLine(String.Format("{0} \t | {1} \t | {2} \t | {3}",
// call the objects from their index
reader[0], reader[1], reader[2], reader[3]));
}
}
}
}
on line 6 of the given code, if my ConnectionString which provides the data souce which I have triple checked and confirmed is proper, the only thing I'm not sure is what type of slash should I use, since the default \ slash from windows directories gets mistaken for an escape by Visual Studio, while with the / slash its ok.
The problem is the connection to the DB always fails with this error
An unhandled exception of type 'System.Data.SqlClient.SqlException'
occurred in System.Data.dll
Additional information: A network-related or instance-specific error
occurred while establishing a connection to SQL Server. The server was
not found or was not accessible. Verify that the instance name is
correct and that SQL
Here is the full list of the namespaces we are using for this application
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 System.Data.SqlClient;
I have tried adding provider keyword to the connection string but that leads to another error about an unsupported keyword, since the System.Data.SqlClient namespace is allegedly automatically setting a provider.
Any help as to what might be causing this is appreciated!
PS: I apologize for any further lack of knowledge I present, I really am new to C# programming, but I find it quite interesting and exciting, and would like to learn more.

You are making a common mistake here. The classes defined in the namespace System.Data.SqlClient (SqlConnection, SqlCommand etc.) are able to talk only with a Sql Server database system (Full, Express or LocalDb). They cannot work with an Access database.
For this database you should use the classes in the System.Data.OleDb namespace (OleDbConnection, OleDbCommand etc.) These classes understand the connectionstring to reach an Access database and can open and work with it.
So your code should be:
....
using System.Data.OleDb;
private void search_db(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection())
{
conn.ConnectionString = ......
conn.Open();
string cmdText = #"SELECT *
FROM Customers
WHERE ([name] LIKE #q1) OR (EGN LIKE #q2)";
using(OleDbCommand command = new OleDbCommand(cmdText, conn))
{
command.Parameters.Add("#q1", OleDbType.VarWChar).Value = search_query;
command.Parameters.Add("#q2", OleDbType.VarWChar).Value = search_query;
using (OleDbDataReader reader = command.ExecuteReader())
{
....
}
}
}
}
An importat thing to remember with OleDb is that you have positional parameters. The parameters are not recognized by their name but by their position in the query text. So if you have two parameters placeholder, even if they are for the same value, you need to put two parameters in the collection. One for the first placeholder and one for the second placeholder. This is even more important when you have placeholders for different values. You need to add the values in the exact order in which they are expected in the query text.

Related

Writing queries for a PostgreSQL DB

To start, here's what I got so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql; // Npgsql .NET Data Provider for PostgreSQL
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
NpgsqlConnection conn = new NpgsqlConnection ("Server=127.0.0.1;Port=5432;UserId=postgres;Password=test;Database=dbab;") ;
conn.Open() ;
// Define a query
NpgsqlCommand cmd = new NpgsqlCommand("set schema 'data'; select test_run.id from test_run;", conn);
cmd.ExecuteNonQuery();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader.GetString(0));
}
}
// Close connection
conn.Close();
System.Console.WriteLine("Press any key to close...");
Console.ReadKey();
}
}
}
Whenever I run this, I get NpgsqlException was unhandled. Under that it says:
An unhandled exception of type 'Npgsql.NpgsqlException' occurred in Npgsql.dll
Additional information: External component has thrown an exception.
When I click view detail, this shows: {"42P01: relation \"test_run\" does not exist"}
When I saw this, I thought maybe my query somehow messed up, but it actually is fine because I ran it in pgAdmin with no problems finding 'test_run' table.
I installed Npgsql -Version 3.0.5 and Mono.Security 3.2.3.0 with Nuget. I also added to two .dll's (not sure how to check this) into the GAC.
In addition, I uninstalled Npgsql, installed it again, but instead got version 2.2.3, and still ran into the same problem. I'm now back with version 3.0.5.
I'm not sure how to fix this, please help.
EDIT:
As jackmott said, I needed to split up the queries. Here's what I did:
NpgsqlCommand cmd = new NpgsqlCommand("set schema 'data';", conn);
cmd.ExecuteNonQuery();
NpgsqlCommand cmd2 = new NpgsqlCommand("select test_run.id from test_run;", conn);
cmd2.ExecuteNonQuery();
And it works.
instead of set schema try:
set search_path to 'schema'
Which is what set schema should alias to, but perhaps that alias doesn't work in NpgSQL
The other thing I would try is to run the set search_path as it's own command, then run the query as the next command..
Third thing to check, is if the user has permissions to read that table in that schema.

Run an SQL script to a certain Database or Table in C#?

How do I run an SQL script to a certain Database or Table in C#?
This is my current setup:
I have a server with multiple databases inside. I want to run an SQL script on a certain Database or Table, How do I run the script in C# wherein I could define in which database/table the script should run? Thanks
You can try USE statement. That is, try using the database name before running the sql script something like,
USE DatabaseName;
SELECT * FROM TableName;
Otherwise you could also try associating the database name in the tablename like,
SELECT * FROM DatabaseName.TableName;
Hope this helps...
Just one of many ways, not the cleanest: When you create your SqlCommand set the ConnectionString directly in its constructor.
SqlCommand Class
SqlConnection Class (your ConnectionString)
To run script from C# you should right the script like this. Here you are defining the database name as 'Initial Catalog'.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.IO;
using System.Data.SqlClient;
public partial class ExcuteScript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sqlConnectionString = #"Initial Catalog=northwind;Data Source=subhash\SQLEXPRESS;Integrated Security=SSPI;Persist Security Info=False;";
string script = File.ReadAllText(#"E:\Project Docs\TACT\northwinddata.sql");
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
server.ConnectionContext.ExecuteNonQuery(script);
}
}
After that your sql script file should manage the requirements.like which table you need to use in your query and you can also connect the tables of other databases using '.' operator syntax is like this.
select * from [database].[schema].[table] where <condition>

how to add, edit, delete and queries since other computer (with internet)

this is my connection class, i know i am going to put the ip, or i am in a error ? the direction of internet? what do i go to put? can i do it with web service? what is the form easy for to do it? here my connection class...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace MisNotas.Conexiones
{
class Conexion
{
public SqlConnection conectar()
{
SqlConnection con = new SqlConnection(#"Data Source=.\sqlexpress;Initial Catalog=misnotas;Integrated Security=True");
return con;
}
public void EjecutarConsulta(SqlCommand comando)
{
comando.Connection.Open(); // open connection
comando.ExecuteNonQuery(); // execute the query
comando.Connection.Close(); // close the connection
}
}
}
The idea is since another place, i can to start session and to do queries and edits... how to do it?
it is in windows application not in asp.net.. thanks
i will put diferents tags for the import is how to do it... thanks
I think the question is how to access a database on a different machine, if that is the case - you can specifiy the IP address or name of the remote machine in the connection string:
public SqlConnection Conectar(string remoteMachine)
{
SqlConnection con = new SqlConnection(string.Format(#"Data Source={0}\sqlexpress;Initial Catalog=misnotas;Integrated Security=True", remoteMachine));
return con;
}
Note however you still have to figure out security, i.e. integrated security might not work remotely - for a full list of the options with examples check out this link.
I think you are asking about the database connection string?
"server=123.123.123.123;database=database_name;user=user_name;password=password"
That is the basic that works for just about any ADO accessible database, but of course, if you are accessing a server where the Windows NTLM mechanism such that the user identity under which your software is a user for that database can work, Integrated Security = true is best.

Which Namespaces Must Be Used to Connect to SQL Server with ADO.NET?

i am using this example to connect c# to sql server. can you please tell me what i have to include in order to be able to use sqlconnection?
it must be something like:
using Sqlconnection; ???
string connectionString = #"Data Source=.\SQLEXPRESS;AttachDbFilename=""C:\SQL Server 2000 Sample Databases\NORTHWND.MDF"";Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection sqlCon = new SqlConnection(connectionString);
sqlCon.Open();
string commandString = "SELECT * FROM Customers";
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
SqlDataReader dataReader = sqlCmd.ExecuteReader();
while (dataReader.Read())
{
Console.WriteLine(String.Format("{0} {1}", dataReader["CompanyName"], dataReader["ContactName"]));
}
dataReader.Close();
sqlCon.Close();
using System.Data;
using System.Data.SqlClient;
If you use SQL Server 2005 or SQL Server 2008 and use C# then you must provide the following namespace.
using System.Data;
using System.Data.SqlClient;
One nice trick one when you don't know what namespace to use in using statement. Type the classname (e.g. SqlConnection, MemoryStream, FileStream, etc), then press Ctrl+.. Though it won't work if the classname doesn't match the case, e.g. Sqlconnection, sqlconnection
Also, check out connectionstrings.com - it's probably the most useful tech website on the internet for making sure your ADO.NET is set up right.
When you decide to move on from just using Northwind, that site will come in handy a bunch. No one I know actually remembers the syntax for a connection string.
Just include the following namespace:-
using System.Data.SqlClient;

What is the best way to connect and use a sqlite database from C#

I've done this before in C++ by including sqlite.h but is there a similarly easy way in C#?
I'm with, Bruce. I AM using http://system.data.sqlite.org/ with great success as well. Here's a simple class example that I created:
using System;
using System.Text;
using System.Data;
using System.Data.SQLite;
namespace MySqlLite
{
class DataClass
{
private SQLiteConnection sqlite;
public DataClass()
{
//This part killed me in the beginning. I was specifying "DataSource"
//instead of "Data Source"
sqlite = new SQLiteConnection("Data Source=/path/to/file.db");
}
public DataTable selectQuery(string query)
{
SQLiteDataAdapter ad;
DataTable dt = new DataTable();
try
{
SQLiteCommand cmd;
sqlite.Open(); //Initiate connection to the db
cmd = sqlite.CreateCommand();
cmd.CommandText = query; //set the passed query
ad = new SQLiteDataAdapter(cmd);
ad.Fill(dt); //fill the datasource
}
catch(SQLiteException ex)
{
//Add your exception code here.
}
sqlite.Close();
return dt;
}
}
There is also an NuGet package: System.Data.SQLite available.
Microsoft.Data.Sqlite by Microsoft has over 9000 downloads every day, so I think you are safe using that one.
Example usage from the documentation:
using (var connection = new SqliteConnection("Data Source=hello.db"))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
#"
SELECT name
FROM user
WHERE id = $id
";
command.Parameters.AddWithValue("$id", id);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var name = reader.GetString(0);
Console.WriteLine($"Hello, {name}!");
}
}
}
I've used this with great success:
http://system.data.sqlite.org/
Free with no restrictions.
(Note from review: Original site no longer exists. The above link has a link pointing the the 404 site and has all the info of the original)
--Bruce
There is a list of Sqlite wrappers for .Net at http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers. From what I've heard http://sqlite.phxsoftware.com/ is quite good. This particular one lets you access Sqlite through ADO.Net just like any other database.
There's also now this option: http://code.google.com/p/csharp-sqlite/ - a complete port of SQLite to C#.
https://github.com/praeclarum/sqlite-net is now probably the best option.
Another way of using SQLite database in NET Framework is to use Fluent-NHibernate.
[It is NET module which wraps around NHibernate (ORM module - Object Relational Mapping) and allows to configure NHibernate programmatically (without XML files) with the fluent pattern.]
Here is the brief 'Getting started' description how to do this in C# step by step:
https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started
It includes a source code as an Visual Studio project.
Here I am trying to help you do the job step by step: (this may be the answer to other questions)
Go to this address , down the page you can see something like "List of Release Packages". Based on your system and .net framework version choose the right one for you. for example if your want to use .NET Framework 4.6 on a 64-bit Windows, choose this version and download it.
Then install the file somewhere on your hard drive, just like any other software.
Open Visual studio and your project. Then in solution explorer, right-click on "References" and choose "add Reference...".
Click the browse button and choose where you install the previous file and go to .../bin/System.Data.SQLite.dll and click add and then OK buttons.
that is pretty much it. now you can use SQLite in your project.
to use it in your project on the code level you may use this below example code:
make a connection string:
string connectionString = #"URI=file:{the location of your sqlite database}";
establish a sqlite connection:
SQLiteConnection theConnection = new SQLiteConnection(connectionString );
open the connection:
theConnection.Open();
create a sqlite command:
SQLiteCommand cmd = new SQLiteCommand(theConnection);
Make a command text, or better said your SQLite statement:
cmd.CommandText = "INSERT INTO table_name(col1, col2) VALUES(val1, val2)";
Execute the command
cmd.ExecuteNonQuery();
that is it.
Mono comes with a wrapper, use theirs!
https://github.com/mono/mono/tree/master/mcs/class/Mono.Data.Sqlite/Mono.Data.Sqlite_2.0 gives code to wrap the actual SQLite dll ( http://www.sqlite.org/sqlite-shell-win32-x86-3071300.zip found on the download page http://www.sqlite.org/download.html/ ) in a .net friendly way. It works on Linux or Windows.
This seems the thinnest of all worlds, minimizing your dependence on third party libraries. If I had to do this project from scratch, this is the way I would do it.
if you have any problem with the library you can use Microsoft.Data.Sqlite;
public static DataTable GetData(string connectionString, string query)
{
DataTable dt = new DataTable();
Microsoft.Data.Sqlite.SqliteConnection connection;
Microsoft.Data.Sqlite.SqliteCommand command;
connection = new Microsoft.Data.Sqlite.SqliteConnection("Data Source= YOU_PATH_BD.sqlite");
try
{
connection.Open();
command = new Microsoft.Data.Sqlite.SqliteCommand(query, connection);
dt.Load(command.ExecuteReader());
connection.Close();
}
catch
{
}
return dt;
}
you can add NuGet Package Microsoft.Data.Sqlite

Categories

Resources