windows forms connect to local database - c#

I have created a very basic windows form application using visual studio 2015.
A service based database has been added (mdf database), and a table has been created..
The database has been added to the service explore, and i can see the connection string in the setting file.
I am simple trying to open a connection to the database to be able to write to it.. Cant seem to find any basic example..
Tried to use this :-
using (var con = new
SqlConnection(Properties.Settings.Default.MyConnectionString))
{
con.Open();
}
}
but no luck. Seem to get the following error
A database with the same name exists, or specified file cannot be opened, or it is located on UNC share."}
my connection string :-
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirector‌​y|\Data\mydb.mdf;Int‌​egrated Security=True

Related

How to open SQLite database while debugging in VS 2019 C# UWP

I've read a ton of articles on this topic but still not able to overcome the problem.
Standard code to open a database:
using Microsoft.Data.Sqlite;
string _dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "orders.db");
using (SqliteConnection db = new SqliteConnection($"Filename={_dbPath}"))
{
db.Open();
etc.
}
The system doesn't produce any error and the databse has Open state. But it's not my database! It seems the system open new empty database (I can create tables, isert/read records there) instead.
Can anybody explain me what I should do to be able to work with my existing database?
I set Mode=ReadWrite - now the new database is not created and produces the error: cannot open db
The database is located in the project root directory.

don't know Connection string parameter in C#

i am developing an application in C#,i have create database1.mdf by
right clicking on my project,add item
then creating a database.
my intial problem was,i was able to execute query correctly but those modification wasn't reflected in main database
finally after lot of surfing i changed copy to output directory to copy if newer but still i am not able to rectify my problem.
now i am feeling that i need to connect to sqlserver using connectionstring but i don't know username,servername,password etc...
can anyone help me please
if you want to connect database in csharp so visual studio will help you to connect with database
go to solution explorer right click on project and add new item and select service-base database click add.
Now Go to Server explorer look there is database with .mdf extension
expand it right click on table and create table your self.
After creating table you need connection string to connect your application to database.
you connection string look like this
string databaseFileName="Your database file with .mdf extension like database1.mdf";
string connectionString=#"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\"+databaseFileName+";Integrated Security=True";
SqlConnection conn=new SqlConnection(connectionString);
conn.Open();
Console.writeLine(conn.State.ToString());/*this will show your database state is open or close.
Note:Here i am using visual studion 2013 other (LocalDB)\v11.0 this could be changed .

how to find connection string sql server via code not by properties

Can someone help me out?
Whenever I switch computers I have to copy the connection string again. I don't want to do that. Is there any alternative method for finding it? I am using Visual Studio 2013 and C#.
I have kept the application in a flash drive so I may use it on any computer. But it only works on my computer.
con = new SqlConnection(#"Data Source=(LocalDB)\v11.0;AttachDbFilename=H:\DataBaseApp\DataBaseApp\DB.mdf;Integrated Security=True");
con.Open();
This is what I'm using but I would appreciate if someone tells me how not to use this.
1) Create connection string property
2) Add connection string to web config. DataSource should be some alias like TestDataBase.
3) Fill the connection string property from web config value.
4) Add Sql Alias on the current PC with name TestDataBase.
When you go to another PC you should only configure new SQL Alias.
In this case multiple users can work without changes in the code(Example source control).
You can save your connection string in any text file, for example: conn.txt and put it in bin\Debug folder. Now, you can read it at runtime by using this code.
string path = Path.Combine(Application.StartupPath, "conn.txt");
string connStrg = System.IO.File.ReadAllText(path);
using (SqlConnection cnn = new SqlConnection(connStrg))
{
...
}
When you publish your application you need to just create that file where the application is installed. I mean if your application is installed in any specific drive like D:\\MyFolder\\MyApp.exe then your conn.txt file should be created on D:\\MyFolder.

Connecting C# app with Oracle 10g: ORA-12154: TNS:could not resolve the connect identifier specified

I am a beginner in working with databases. I am trying to access Oracle10g database from a c# application. But when I do so i get this error:
ORA-12154: TNS:could not resolve the connect identifier specified"
I'm using the following code:
string oradb = "Data Source=ORCL;User Id=system;Password=goodbye;";
OracleConnection conn = new OracleConnection(oradb); // C#
conn.Open();
Is there an error in the connection string oradb?
Start the Visual Studio, open View menu + Server Explorer.
Right mouse click on Data Connection + Add Connection + Select Oracle Database
server Name : localhost or name of your machine, set username & password and click on Test Connection to verify the above parameters. Press OK if test is succeeds.
From properties windows you can obtain connection String and it should be look a like:
Data Source=localhost;Persist Security Info=True;User ID=scott;Password=***********;Unicode=True
Oracle is just stating that it can't find the database.
If you're running a local Express Edition database, you should be able to just use XE as an instance name, and everything should already be set up, otherwise you can most easily add it to tnsnames.ora.
To find the correct tnsnames.ora to change, you can try (from the command prompt)
tnsping ORCL
That will tell you which files Oracle is using to try to find the database. If tnsping is an unknown command, you may have to search for it and go to the correct place before running it.
One you found the correct tnsnames.ora, you need to add the instance ORCL to it. There should be an existing file with examples, the syntax of that file is too complex to answer here, if you need help, Oracle has quite extensive documentation.
This is a very common oracle error. Simply put, it means that you have named the database you wish to be connected to and Oracle doesn’t know who the heck you’re talking about. I suggest 6 Steps to fix ORA-12154:
Check instance name has been entered correctly in tnsnames.ora.
There should be no control characters at the end of the instance or database name.
All paranthesis around the TNS entry should be properly terminated
Domain name entry in sqlnet.ora should not be conflicting with full database name.
If problem still persists, try to re-create TNS entry in tnsnames.ora.
At last you may add new entries using the SQL*Net Easy configuration utility.
More informations on oracle site or here : http://turfybot.free.fr/oracle/11g/errors/ORA-12154.html

How to create an installation that contains a sql server database

I Created an application in c# with Visual Studio 2008, and i'am using SQL Server 2012 for implement databases for the applications.
My Question is :
How can i create an installation that contains the database without using the atach databse method.
Please F1 ! F1 !
You can export a script that will create and populate the database with data. What is the setup tool you are using ?
Or you can make the C# app create the database and default data in the first launch. In both cases you have to make a script file to create/populate the database.
To export the SQL script
right click on the database name in the SQL server manager
tasks->script
in C# open the scrip file using StreamReader and read all the lines. Then using SqlCommand execute the script file. First you need to open connections to Master database as you do not have your database created yet. Then modify the connection string and replace the master database with your database name.
The connection string can be stored in the Application Settings and you can provide a settings dialog to modify it for simplicity make the default connection string connect to the localhost computer and your database name.
The following code modify a setting key names Setting1
MessageBox.Show(Properties.Settings.Default.Setting1);
Properties.Settings.Default.Setting1 = "New Value";
Properties.Settings.Default.Save();
You can also create a custom action in your installer (.msi) file - here's a link that tells you the steps to do that. adding link to example with C# - here

Categories

Resources