I'm currently using VS2015 and a local instance of MySQL Server (managed through Workbench) to develop a school project.
The project is a Web form app (.NET/C#) which I have never really done before. I have done C# with a MySQL connection in the past but I think it was just a Windows form app, and I had no issues connecting the database to the form.
I'm currently trying to use a GridView element from the toolbox in Visual Studio to connect to my database. When I go in and edit the source settings, I can create a connection and it will see the tables in my database.
I can then click the "Test Connection" button and it will succeed in connecting. However, if I go forward and try to select items to view from the table, and then click "Test Query" I get the following error:
I have Googled the error and tried some of the solutions found on SO like installing the Clear MEF Cache extension, but it didn't solve anything.
Additionally, I've created some functions from scratch to connect and insert data. They also have problems in that trying to insert any more than one value into the table will fail. Like INSERT INTO user_info(user_id, user_name) VALUES ('5', "Bob") will insert the user_id but not the user_name.
Please see here for a tutorial to connect to MySQL and here for filling your DataGridView from MySQL. Make sure you have the correct reference added to your project as well (References > MySQL.Data).
I was able to create the SQL database tables using "schema compare" in visual studio. I added the login & user to the database directly using T-SQL, and also used T-SQL to add the user to datareader & datawriter roles.
Now, when I do a "schema compare" in visual studio using my project as the source, it wants to delete the user and roles from the database. How do I add these to the project?
It depends if you are using the new Sql Server Data Tools (SSDT) project (.sqlproj) or the older 2010 SQL Database Project (.dbproj).
If you are using the new SSDT project - then you just add the objects into the project:
Create a file for your new Login
Create a file for your new DB User
Create a file called YourDatabaseName.rolememberships.sql
That file will have code in it like:
EXECUTE sp_addrolemember #rolename = N'db_datareader', #membername = N'NewDBUser';
And you will need a Permissions.sql file. In there you will grant the Login connect rights like this:
GRANT CONNECT TO [NewLogin] AS [dbo];
If however you are using the old database project you will need to create a new project in your solution called a Server project. It looks very much like your database project, except it is of the master database.
You do similar things in this new server project as I outlined above and in your original database project you will need to add the server project as a reference.
A caveat to this whole process. You will want to add the Login with the SID value so that you don't end up with orphaned user scenarios when you try and put a backup of your database on some other server.
I have used the process outlined in this MS KB to get the SID in the first place and then if you refer to the BOL section about CREATE LOGIN you will see what the syntax should be for creating your login file.
I've installed MYSQL server, running local on 127.0.0.1
(I've also tried this with MySQL server at a public IP with same results).
Here is how I'm configuring it:
Using MySQL Connector / ODBC (running localhost mysql server) using Visual Studio 2013 C#
I click Design tab of a webform.aspx page - and click and drag (from Toolbox->Data->GridView into the MainContent frame, and drop.
I then click the [>] button
Under Choose Data Source, I choose to create a new data source.
Under Data Source Configuration Wizard, I choose SQL Database.
Next pops up a window, Configure Data Source - SqlDataSource1
I click New Connection
Microsoft ODBC Data Source (ODBC) is selected as Data source - I leave it (note: here, I have tried changing this to MySql Data Source - but the connection fails). so I use the ODBC option.
I click the () Use Connection string: and click [Build]
A window pops up, and I select the Machine Data Source tab
I have already created a Data Source named test_localhost_mysql, I select it.
A window with MySQL Connector/ODBC connection parameters appears, with all of my connection data. I click Test.
success.
I click [OK]
Now, the Add Connection - Use connection string, is filled in, along with my login information.
I click Test Connection
Success.
I click [OK]
configuration wizard is now complete, and shows a string.
I click [NEXT]
I click [NEXT] without changing anything
Now is when it becomes interesting. on this screen;
under Specify columns from a table or view - all my tables are in the drop down list. Periodically, the columns will show here as well. Currently they aren't -- I'm sure this is not random, although I'm not sure what I'm doing to cause it.
I click [Next] after selecting my table.
I click [TEST]
It fails.
I am sure it's something simple, but I promise I've been through everything I can think of, and can really use some help please.
Anyone that can guide me I would greatly appreciate it.
I anxiously await your genious :) Thank you!
Check this out. Try to install with the instructions here. Report back if the issue persists.
http://www.w3resource.com/mysql/mysql-odbc-connection.php
EDIT: You also might want to run through all of your SQL code. It clearly says that the SQL syntax is not correct.
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
I have created a database with SQL Server Management Studio, I would like to now use it in my C# application. I need the connection string?
Where can I find the connection string, and where is my database stored?
Do I have to publish it or something like that, or is it in my documents somewhere?
using (var conn = new SqlConnection("your connection string to the database"))
How do I obtain the connection string? Where can I find the connection string to copy paste into the above section?
How to I publish my database so that Visual Studio can pick it up? Then I can just pull the connection string of there?
The easiest way to get the connection string is using the "Server Explorer" window in Visual Studio (menu View, Server Explorer) and connect to the server from that window.
Then you can see the connection string in the properties of the connected server (choose the connection and press F4 or Alt+Enter or choose Properties on the right click menu).
Advanced connection string settings: when creating the connection, you can modify any of the advanced connection string options, like MARS, resiliency, timeot, pooling configuration, etc. by clicking on the "Advanced..." button on the bottom of the "Add connection" dialog. You can access this dialog later by right clicking the Data Connection, and choosing "Modify connection...". The available advanced options vary by server type.
If you create the database using SQL Server Management Studio, the database will be created in a server instance, so that, to deploy your application you'll have to make a backup of the database and deploy it in the deployment SQL Server. Alternatively, you can use a data file using SQL Server Express (localDB in SQL Server 2012), that will be easily distributed with your app.
I.e. if it's an ASP.NET app, there's an App_Datafolder. If you right click it you can add a new element, which can be a SQL Server Database. This file will be on that folder, will work with SQL Express, and will be easy to deploy. You need SQL Express / localDB installed on your machine for this to work.
A very simple way to retrieve a connection string, is to create a text file, change the extension from .txt to .udl.
Double-clicking the .udl file will open the Data Link Properties wizard.
Configure and test the connection to your database server.
Close the wizard and open the .udl file with the text editor of your choice and simply copy the connection string (without the Provider=<driver>part) to use it in your C# application.
sample udl file content
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;User ID="";Initial Catalog=YOURDATABASENAME;Data Source=YOURSERVERNAME;Initial File Name="";Server SPN=""
what you need to copy from it
Integrated Security=SSPI;Initial Catalog=YOURDATABASENAME;Data Source=YOURSERVERNAME;
If you want to specify username and password you can adopt from other answers.
Tutorial: https://teusje.wordpress.com/2012/02/21/how-to-test-an-sql-server-connection/
Open SQL Server Management Studio and run following query. You will get connection string:
select
'data source=' + ##servername +
';initial catalog=' + db_name() +
case type_desc
when 'WINDOWS_LOGIN'
then ';trusted_connection=true'
else
';user id=' + suser_name() + ';password=<<YourPassword>>'
end
as ConnectionString
from sys.server_principals
where name = suser_name()
If you have installed and setup MS SQL Server and Management Studio, go to Visual Studio (Visual Studio not SQL Server Management Studio).
1] In Visual Studio go to Tools -> Connect to Database.
2] Under Server Name Select your Database Server Name (Let the list Populate if its taking time).
3] Under Connect to a Database, Select Select or enter a database name.
4] Select your Database from Dropdown.
5] After selecting Database try Test Connection.
6] If Test Connection Succeeds, Click Ok.
7] In Visual Studio go to View -> Server Explorer.
8] In Server Explorer window, Under Data Connections Select your Database. Right Click your Database -> Click Properties.
9] In Properties window you will see your Connection String.
On connectionstrings.com you can find the connection string for every DB provider. A connection string is built up with certain attributes/properties and their values. For SQL server 2008, it looks like this (standard, which is what you'll need here):
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
on myServerAddress, write the name of your installed instance (by default it's .\SQLEXPRESS for SQL Server Express edition). Initial catalog = your database name, you'll see it in SSMS on the left after connecting. The rest speaks for itself.
edit
You will need to omit username and password for windows authentication and add Integrated Security=SSPI.
My solution was to use excel (2010).
In a new worksheet, select a cell, then:
Data -> From Other Sources -> From SQL Server
put in the server name, select table, etc,
When you get to the "Import Data" dialog,
click on Properties in the "Connection Properties" dialog,
select the "Definition" tab.
And there Excel nicely displays the Connection String for copying
(or even Export Connection File...)
If one uses the tool Linqpad, after one connects to a target database from the connections one can get a connection string to use.
Right click on the database connection.
Select Properties
Select Advanced
Select Copy Full Connection String to Clipboard
Result: Data Source=.\jabberwocky;Integrated Security=SSPI;Initial Catalog=Rasa;app=LINQPad
Remove the app=LinqPad depending on the drivers and other items such as Server instead of source, you may need to adjust the driver to suit the target operation; but it gives one a launching pad.
put below tag in web.config file in configuration node
<connectionStrings>
<add name="NameOFConnectionString" connectionString="Data Source=Server;Initial Catalog=DatabaseName;User ID=User;Password=Pwd"
providerName="System.Data.SqlClient" />
then you can use above connectionstring, e.g.
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["NameOFConnectionString"].ToString();
The sql server database will be stored by default in the following path
<drive>:\Program Files\Microsoft SQL Server\MSSQL.X\MSSQL\Data\
, where <drive> is the installation drive and X is the instance number (MSSQL.1 for the first instance of the Database Engine). Inorder to provide the connection string you should know what is the server name of the sql server database, where you have stored followed by instance of the database server.
Generally the server name will be like the ip address of the machine where the database is attached and the default instance will be SqlExpress
A connection string contains Data Source name i.e., server name, Initial catalog i.e., database name, user id i.e., login user id of the database, password i.e., login password of the database.
Easiest way my friends, is to open the server explorer tab on visual studio 2019 (in my case), and then try to create the connection to the database. After creating a succesful connection just right click on it and go to propierties. There you will find a string connection field with the correct syntax!...This worked for me because I knew my server's name before hand....just couldn't figure out the correct syntax to run my ef scaffold...
If you created Connection Manager in your project then you can simply pull the connection string from there.
String connection = this.dts.connections["<connection_manager_name>"];
And use this connection in:
using (var conn = new SqlConnection(connection))
Please correct me if I am wrong.
SqlConnection con = new SqlConnection();
con.ConnectionString="Data Source=DOTNET-PC\\SQLEXPRESS;Initial Catalog=apptivator;Integrated Security=True";