How to find out SQL Server default instance name? - sql

I have three SQL server instances on my computer. How can I find out which is the default one?
var conn = new SqlConnection("Data Source=.;Initial Catalog=SampleDb;Integrated Security=True")
I want to learn when I use '.' as above which database I will use.

You can only have more than one instance per OS installation if all of them have different instance names. The data source property has the format
HOST\INSTANCENAME
You are connecting to the default instance (of which there can only be one).

Related

Multiple readers access database

I've read about an option in the connection string called MARS (MultipleActiveResultSets), but from what I've gathered it is only a valid argument for Sql Server 2005. Is there an equivalent setting for OleDB to open an access database with multiple readers?
I am aware that I can create multiple connections, one for each reader. I am currently doing this, but it would preferable to execute both readers on a single connection. Here's my current connection string:
"Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\MyDb.mdb;Jet OLEDB:Database Password=MyPassword"

Connect SQL Express 2012 vb.net

I have been working on this for a few days now. I am trying to make vb.net forms app for my database. Right now I am working on a login form and a main form. I have researched many different websites and played with this string forever, but i can not get a connection to my db. I get different errors saying the machine refused it, then ill get a timeout error, then it will go back to refusal.
Dim conn As New MySqlConnection
If conn.State = ConnectionState.Closed Then
conn.ConnectionString = ("Server=192.168.0.2;Database=Sunshinetix;User=sa;Password=sunshine;")
End If
Can someone please tell me what I am doing wrong? I am a beginner in vb.net. And this is a remote server, but LAN.
Thanks!
PS: Is is because I am using SQL Express?
Enable remote connections for SQL Server Express 2012
I've had that article open for a few days and it has helped me a lot to this point.
If you are using Sql Server express, you need to use a SqlConnection, not a MySqlConnection. That one is for use with MySQL, which is a different implementation of SQL than Microsoft SQL Server
Of course, Microsoft also offers the OleDbConnection as kind of a "catch-all" (see more here: Difference between Sql Connection and OLEDB Connection). Still, if I know I'm going to stick with a particular SQL provider, then I generally use the specialized version.
Also, if you're ever interested, here is a small list of common SQL providers:
Microsoft SQL Server - proprietary SQL provider developed and supported by Microsoft
MySQL - Free, open source relational database system developed by Oracle. Very popular with web developers.
PostgreSQL - another open-source RDBMS. Gaining popularity due to its flexibility and adherence to standards.
SQLite - a small, SQL provider with an emphasis on portability. Unlike the others, it uses local database files rather than a remote server. This has made it pretty much the default choice when developing mobile applications that require local storage.
Try this:
Dim SQLConn As SqlConnection = New SqlConnection
SQLConn.ConnectionString = "Data Source=servername;" & _
"Initial Catalog=databasename;" & _
"User ID=username;" & _
"Password=userpassword;"
The reason why you can't connect is that you are using MySQLConnection that is connectiong to connecting to MySQL DB which is different from MS SQL so you need to use SqlConnection
You might also check this:
http://support.microsoft.com/kb/308656
Also check this site:
http://www.connectionstrings.com/sql-server/
To know preferences of your SqlConnection string according to SQL version.
Try this to test your connection string:
Dim connectString as String = ""
Try
Dim objConn As SqlConnection = New SqlConnection(Server=192.168.0.2;Database=Sunshinetix;User=sa;Password=sunshine;)
objConn.Open()
objConn.Close()
Msgbox("Successfully connected to database!")
Catch ex As Exception
Msgbox("Cannot connect, Error:" & ex.Message)
End Try

Connect VB with SQL (Local + unknown database name)

Basically I want to know if it is possible to connect my VB app with SQL server on the same Computer (Local) and interrogate it for a list of database names.
I just want to know if that is possible.
sqlConn.ConnectionString = Data Source=local;Initial Catalog=unknownDatabase;User Id=knownUsername;Password=knownPassword;"
Fill DataBase Names to Drop Down
User Selects DB Name
Change Connection to now Known DatabaseName
Of course it is possible.. You can create a connection to a local SQL instance the same way you'd create a connection to a remote one.
Then to get a list of the databases on the server, execute "SELECT Name FROM master.sys.databases"

How do I refer to SQL Server database with SqlDataAdapter

I'm new to C# and I'm learning it by following a tutorial.
I've created a SQL Server database (Database1.sdf) with one table and few columns by clicking "Add New Item" on my project.
SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=??????;Integrated Security=True;Connect Timeout=30;User Instance=True");
DataTable dt = new DataTable();
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM Database1 WHERE Titel like " + int.Parse(textBox1.Text), conn);
SDA.Fill(dt);
dataGridView1.DataSource = dt;
As you see the "???" part is where I'm stuck. I tried using the path of the database
C:\Users\1\Documents\Visual Studio 2010\Projects\Opdracht0\Opdracht0\Database1.sdf
But that is not working. When I run it, I get:
Input string was not in a correct format.
and this line is red:
SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM Database1 WHERE Titel like" + int.Parse(textBox1.Text), conn);
As you can see it's a simple SQL statement but even that won't work.
Any tips are appreciated!
Your main problem is you're mixing two incompatible types of databases:
your database1.sdf file is a SQL Server Compact Edition database file
your SqlConnection and SqlCommand however are used for full-fledged SQL Server (Express) versions - and those are NOT compatible with SQL Server Compact!
So either you need to
keep using your SQL Server Compact database (database1.sdf) - but in that case, you need to use SqlCeConnection and SqlCeCommand and those commands
OR THEN:
you switch to a real SQL Server (Express, Standard, Enterprise) and in that case you can use SqlConnection and SqlCommand
To connect to MS SQL Server database from C#.NET, you need to create a connection string such as below:
private SqlConnection connection;
private string connectionString =
#"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );
Next, you use the SqlConnection object created above to create a 'SqlCommand', as shown below:
SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = #Cid", connection);
The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.
Next to execute the SQL queries in the database, you use the following methods:
ExecuteReader - to execute SELECT queries
ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.
This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database.
For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/embedding-sql-c-sharp-java-shahriar/ )
Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.
Here's how I would go about it. I would go to MSDN and type SqlConnection into the search box. After poking around a bit (because they've deep-sixed my favorite connection string examples), I'd find something like this which has some examples like:
Persist Security Info=False;Integrated Security=true;Initial Catalog=Northwind;server=(local)
Data Source=MSSQL1;Initial Catalog=AdventureWorks;Integrated Security=true;
Usually, the server name is the same as the Computer's name, though SQL Express may do this differently. Or, (local) may work here. These connection strings will log into SQL Server with the user's Windows credentials. For a web application the actual user may depend on whether impersonation is used, etc. (And, yes, the connection string should go into the web.config so it can be updated without recompiling the app. If this is a web app.)
At any rate, if this doesn't work for you, you can use the same search strategy to find other connection string examples on MSDN or Google.

SQL server - execute scalar function without specifing db name

I have a user defined SQL function that I am able to call from management studio using syntax dbo.Function(arg)
Now, when I have to call this function from C# if I don't specify **dbname**.dbo.Function(arg) I get an error that SQL server does not find this user defined function. How can I solve this without specifing dbname ? I already connect to the server using a connection string that specifies the "initial catalog = dbname"
It seems that I cannot reproduce mentioned behavior at this point :-) (either using SQL server 2005 or 2008) I have to put this question on hold
Your connection string needs to specify the database to use initially. It might look something like this:
var cn = new SqlConnection(
"SERVER=SomeServer;DATABASE=SomeDb;Integrated Security=SSPI;"
);
Without that, you're probably being dumped into the master database, which is why you need to fully qualify the function name.