Showing posts with label connection. Show all posts
Showing posts with label connection. Show all posts

Friday, March 30, 2012

Need help connecting to server with DSN

Hi all,

I am used to connecting to my database using a querystring and a connection like this:


string connectionString = "server=\'myserver\'; user id=\'myuser\'; password=\'mypw;database=\'mydb\'";
System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(connectionString);

I write in C# and don't know how to connect to a database using a DSN. I have this VB code that's supposed to connect with DSN:

<%
set conn = Server.CreateObject("ADODB.Connection")
conn.open"DSN=MyDSN;UID=user;PWD=password;DATABASE=databasename"
%>

But I need it in C#...

I tried using a query string like this one


string connectionString="DSN=mydsn;UID=myuser;PWD=mypw;DATABASE=mydb";

and got this error :Unknown connection option in connection string: dsn. :

Any help would be greatly appreciated... thanksPlease check the following :


OdbcConnection cn;
OdbcCommand cmd;
string MyString;

MyString="Select * from test";

cn= new OdbcConnection("dsn=myDSN;UID=myUid;PWD=myPwd;");
cmd=new OdbcCommand(MyString,cn);
cn.Open();
......

|||that's classic ASP code in VBScript - not ASP.NET code in VB.NET

Anyway, is there a particular reason you NEED to use a DSN? it IS possible, but it's not the best option.

Saturday, February 25, 2012

Need a connection string for sql server via vb6

Hey guys,
sorry to bother you with something so newbish, but I am trying to connect to a remote sql server db, using vb6.
I want to use ado to do so but have been having problem with some of the samples I have found online.

I will probably just test the connection out on an existing database like pubs or northwind.

Can anyone provide me a code snip please?

This forum has always been a great resource for me, thanks again guys!Driver={SQL Server};Server=localhost;Address=localhost,1433;Ne twork=DBMSSOCN;Database=northwind;Uid=sa;Pwd=;

Switch the appropriate data out

Rob|||You also might want to add www.connectionstrings.com to your favorites.

Need "refresh connection" or something ?

HI,

I have a problem with the data that I want to delete and insert new one on my SQL.
It will work if I just delete a row. And work well if I just insert a row.
But it will not work if I delete and insert at once on one procedure.

Here's the code :

Protected Sub RolesRadioButtonList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RolesRadioButtonList.SelectedIndexChanged
Session("CurrentRoleId") = Me.RolesRadioButtonList.SelectedValue
' Call Sub RemoveUsersInRoles
RemoveUsersInRoles()
' Call Sub AddNewUsersInRoles
AddNewUsersInRoles()
End Sub

Sub RemoveUsersInRoles()
' Create SQL database connection
Dim sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True")
Dim cmd As New SqlCommand
cmd.CommandType = CommandType.Text
' Delete that row with correct UserId
cmd.CommandText = "DELETE aspnet_UsersInRoles WHERE (UserId = '" & Session("CurrentUserId") & "')"
cmd.Connection = sqlConn
sqlConn.Open()
cmd.ExecuteNonQuery()
' Close SQL connecton
sqlConn.Close()
End Sub

Sub AddNewUsersInRoles()
' Create SQL database connection
Dim sqlConn As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True")
sqlConn.Open()
' Create new row with new UserId
Dim sqlString As String = "INSERT INTO aspnet_UsersInRoles (UserId, RoleId) VALUES( '" & Session("CurrentUserId") & "','" & Session("CurrentRoleId") & "')"
Dim sqlComm As New SqlCommand(sqlString, sqlConn)
Dim sqlExec As Integer = sqlComm.ExecuteNonQuery
' Close SQL connection
sqlConn.Close()
End Sub

--------------

I can delete the row if I call 'RemoveUsersInRoles'
And I can insert new one if I call 'AddNewUsersInRoles'

But if 'RolesRadioButtonList_SelectedIndexChanged' is called, it will not work well.
If the row not exist, it will insert new row. And that what I want.
But If the row is exist, It wont delete that row and wont insert the new one. That's the problem.

Do I need some 'pause' or 'refresh connection' here ?

Thank You.

Why don't you create one procedure that checks if the row exists, then delete and insert if it does (or just amends/updates it), otherwise it just performs an insert?

|||

uh, Mike, thanks for your respon.

My mistake. I put this at my page load. It always reset the variable when the page is reload.

If Len(Session("CurrentRoleId")) Then
Me.RolesRadioButtonList.SelectedValue = Session("CurrentRoleId")
End If

And Mike, thanks for the advise : use update. I already use it.

And thank you all at that 2nd class, keep programming ...Stick out tongue

Nebie connection problems with SQL Server 2005 Express and VS 2005

I downloaded SQL Server 2005 Express and some sample databases (incl. Adventureworks_data.mdf), but when I try to Add Connection in VS 2005 to point to this db and use the Test Connection button, it gives the error:


Microsoft Visual Studio

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I've tried setting the data source as either "Microsoft SQL Server Database File" or "Microsoft SQL Server", but get the same error.

Any tips on verifying SQL Srv Express is installed correctly? Any ideas on how to solve this problem?

Thanks in advance.

What happens if you set the source=(local)?