I have an application in C# that connects to a MySql application, and it works great. I need to build the same thing for a Sql database, but I'm find how to build the same connection string.
Which would be the equivalence o this line? Does it remain the same?
Allow User Variables=True;respect binary flags=false; Connection Timeout=10; Pooling=false
Allow User Variables=true: SQL Server has no need for this because it allows user variables by default.
Respect Binary Flags=false: There is no equivalent for this in SQL Server. It makes Connector/NET treat some BLOB columns as TEXT which shouldn't be required if the column types are created correctly in the new DB.
Connection Timeout=10: use ConnectTimeout=10 or Connection Timeout=10 for SqlClient.
Pooling=false: Use Pooling=false for SqlClient.
Related
I am fairly beginner in here, so any help would be much appreciated :)
So, I created a SQL Database and I want to connect EasyTables to it. Apparently the automatic option has been removed and I have to do it manually.
I followed "How can I add a connection string manually" page but it lead me to nothing.
Where should i create the connection string, and what to put in the value field?
Or if there is any tutorials out there for the new way please tell me :)
Thank you a lot
You said you have followed tutorial: How can I add a connection string manually.
The Easy Table connection should like this:
SQL Database Connection String format
Data Source=tcp:{your_SQLServer},{port};Initial Catalog={your_catalogue};User ID={your_username};Password={your_password}
{your_SQLServer} Name of the server, this can be found in the
overview page for your database and is usually in the form of
“server_name.database.windows.net”.
{port} usually 1433.
{your_catalogue} Name of the database.
{your_username} User name to access your database.
{your_password} Password to access your database.
Add the connection string to your Web App
In App Service, you can manage connection strings for your application by using the Configuration option in the menu.
To add a connection string:
Click on the Application settings tab.
Click on [+] New connection string.
You will need to provide Name, Value and Type for your connection
string.
If your are adding a connection string to a SQL Azure database choose
SQLAzure under type.
If your are adding a connection to an Azure Storage account, chose
Custom under type.
NOTE If you are adding a connection string because you are planning on using the Easy API or Easy Table features, then the connection strings used by this features expect the following specific names:
Azure SQL database: MS_TableConnectionString
Azure Storage account: MS_AzureStorageAccountConnectionString
For example, this is my connection string:
When you have configured this, go to Easy table, click Add to add the table name in your Azure SQL database.
You can find that esay table has connected to my Azure SQL database now and we can see the data in the table.
Update:
You should first add the connection string in the configuration followed the format provided for you.
Then go to Easy Table, configure Easy table API:
Choose the 2:
When it done, you can add table in your SQL database, please follow my steps in above.
Note:
Your connection string name must be: MS_TableConnectionString.
Hope this helps.
I have a program in C# which connects to a database using a DSN (ODBC driver).
All queries I run in the program uses SQL. All works well but when using datetime data type in queries (in SELECT statement or in WHERE clause).
When DSN is a MS ACCESS database, I have to use something like this:
WHERE SomeDate = #1/1/2005#
But if the same database is in SQL Server, i should use something like this:
WHERE SomeDate = '1/1/2005'
Is there a way to manage this in the same program without doing a routine for SQL Server and another for MS ACCESS?
I tried by getting properties of OdbcConnection object, but I could not find a way to detect if driver is MS Access or SQL Server.
You probably have used data type Datetime2 in SQL Server. That will be read as text if using the native SQL Server ODBC driver.
So either:
Use the Microsoft® ODBC Driver 11 for SQL Server® - or newer, 17 is current:
Microsoft® ODBC Driver 17 for SQL Server®
Change the data type of the field to DateTime
You can set your Access database to be in ANSI-92 mode. This will allow you to query date fields with single quotes.
A word of warning though if this is an existing database or there are other applications using it. This may introduce a breaking change to any existing queries in your database that are not ANSI compliant. Likewise, if there are other applications using this database their queries may also break.
Also as an alternative, please note that OdbcConnection does have a Driver property that you can probe to determine the data source. On my machine, a DSN pointing to a SQL Server database returns sqlncli11.dll, because I am using the SQL Server Native Client 11.0. You can use this property to drive the syntax of your queries in your application. You could create a data access layer that abstracts some of this away from the rest of your code.
I'm very new to SQL and made a small program where a user can input some data, click submit and the data is then stored in a table in the database.
I know want to move the application onto a friends computer, which i'm assuming has no SQL software installed, what would be the easiest way to do this, when obviously the connection string is unique to my computer and the database is stored on my computer.
You will have to install SQL Server on their machine first and foremost. Once this is done, you can obtain a relevant connection string. Note, for the 'Server name' part of the connection string, if you are using SQL Express, instead of using 'localhost', or the name of the server instance (i.e. 'MyMachine'), you would use 'localhost\SQLEXPRESS'/'MyMachine\SQLEXPRESS'.
After setting up the SQL Server instance on the new machine, to copy the required database, first detach the database to avoid any corruption. Now you are free to merely copy the file from your machine to theirs and go through the usual attachment process using SQL Server Management Studio (SQLMS).
I hope this helps.
You can use SQL CE or other file databases. On this way you need to install SQL CE(you can include SQl CE installer into your program installer) on target computer and after that you can easy copy db-file from you computer to target computer.
Also, you can use relative path to db-file from your exe file instead of fixed connection string:
string dbDirPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"DB" );
private const string CONN_STR_TEMPLATE = "Data Source={0};Persist Security Info=False;";
string dbFilePath = Path.Combine(dbDirPath, "my.sdf");
_connStr =String.Format(CONN_STR_TEMPLATE,dbFilePath);
You cannot simply copy your database since SQL Server database is NOT a standalone database as SQL Compact edition/MS Access.
You may configure your router to remote access SQL Server instance over the Internet by forwarding the port
Accessing SQL Server Instance through NAT
How can I connect my Trgovina.mdf with dataGridView?
I follow this tutorial, but it seems that program doesn't find my database.
Connection string looks like that:
string connString = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Klemen\documents\visual studio 2012\Projects\Trgovina\Trgovina\Trgovina.mdf;Integrated Security=True";
Everything else is the same as tutorial example.
Error string is An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'
Full code looks like this.
The tutorial you talk about in your question use an Access Database and thus uses the OleDB engine to reach and work with the database. Instead your connection string use the syntax reserved for SQLServer LocalDB.
You should change your objects to SqlConnection (instead of OleDbConnection), SqlCommand (instead of OleDbCommand) and so on...
With these changes you should be able to connect to the automatic instance of SqlServer LocalDB. The rest of the tutorial could work or not, depending on what is present in the MDF file used.
You trying to connect to database .mdf file, but you have a wrong provider.
An MDF is a Microsoft SQL Server database not a Jet Database like
Access (*.mdb). You cannot just connect to the flat file and read it.
You would need to mount the database in an instance of SQL Server.
You could install SQL Server 2005 Express
Source
Note: Just download MS SQL Server 2005 Express or later and you must use the System.Data.SqlClient instead of OLE DB to solve your problem.
Currently I am using SQL Server CE for persisting my data for which I am providing with a .sdf and connection string mentioned in app.config pointing to this .sdf file.
Now I want to provide user with the flexibility to have the data stored in their own SQL Server database if present at there disposal.
Now I am facing the problem of how to change the connection string at runtime if user chooses to uses its own database ?
Or if restrict them to use my predefined .mdf file how to attach that in their SQL Server ?
My recommendation would be to have 2 connection strings in the configuration file (app or web). There is a special section for them intuitively called ConnectionStrings. You can then switch between them based on other settings.
Changing connection strings dynamically is actually pretty easy to do as long as you have a place to store the new settings (ie. web or app config files). If you provide a way for them to enter the server information you can use the ConfigurationManager class to update your app/web.config.
Ado.net typically has parameters on almost any db connection object that allows you to specify the connection string as an arguement. Additionally, there are helper classes that can be used to construct the connection string on the fly like the SqlConnectionStringBuilder or EntityConnectionStringBuilder. I personally love the Entity Framework as it allows you to create the database from the model itself if it does not already exist, provided you already have the connection string.