Showing posts with label authetication. Show all posts
Showing posts with label authetication. Show all posts

Monday, 19 March 2012

Connect to .DataSource="...." using Authetication (userid & passphrase)!

Hi,

My school is runnnig SQL Server 2005 and i want to connect to it using authetication (userid & passphrase). As i coded the windows application i used localhost as my own computer and IntegratedSecurity shown below. I'm new in C# and SQL. I would appriciate if someone could help me out.

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Personregister
{
class DBManager
{
SqlConnection conn;
SqlConnectionStringBuilder builder;

public DBManager()
{
builder = new SqlConnectionStringBuilder();
builder.DataSource="localhost";
builder.InitialCatalog = "Personregister";
builder.IntegratedSecurity = true;
conn = new SqlConnection(builder.ToString());

}
// more code fallows ......
}
}

Best Regards,

See the documentation for SqlConnectionStringBuilder at http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder_members.aspx.

For future questions on such topic, you should post on: SQL Server Data Access

Thanks
Laurentiu

Sunday, 11 March 2012

connect from webservice

I have a webservice I am creating. I am needing to connect to a SQL server on
a different machine using SQL authetication. I have the connection string
located in the machine.config. Could somebody give me a sample of how to use
that connection string to connect to the database from inside a webservice.
It's not any different than connecting from an ASP.NET page so if you know
how to do that, you should be all set. It's not a good practice to place
passwords in a config file so you should look into using Windows
authentication or at the very least encrypt the password in the config file.
Roman
Roman Rehak
http://sqlblog.com/blogs/roman_rehak
"wendld" <GenieLifts@.news.postalias> wrote in message
news:7A009E7B-EE22-4FFC-8B4B-FE870659D985@.microsoft.com...
>I have a webservice I am creating. I am needing to connect to a SQL server
>on
> a different machine using SQL authetication. I have the connection string
> located in the machine.config. Could somebody give me a sample of how to
> use
> that connection string to connect to the database from inside a
> webservice.
|||This is my first attempt at pulling in the connection string from the
machine.config. Windows Authentication is not an option since the database
and webservers are on different domains and a DMZ is in use. I currently have
the SSL encryption on, but have not encrypted the actually value in the
config file (yet). That is the next step after I get the programs working
this way.
"Roman Rehak" wrote:

> It's not any different than connecting from an ASP.NET page so if you know
> how to do that, you should be all set. It's not a good practice to place
> passwords in a config file so you should look into using Windows
> authentication or at the very least encrypt the password in the config file.
> Roman
> --
> Roman Rehak
> http://sqlblog.com/blogs/roman_rehak
>
> "wendld" <GenieLifts@.news.postalias> wrote in message
> news:7A009E7B-EE22-4FFC-8B4B-FE870659D985@.microsoft.com...
>
>
|||in web.config:
<appSettings>
<add key="ConnectionString" value="Data Source=MyServer;Initial
Catalog=AdventureWorks;User Id=MyUser;Password=MyPassword;"/>
</appSettings>
in the web service:
string cnString =
ConfigurationManager.AppSettings.Get("ConnectionSt ring");
Roman
Roman Rehak
http://sqlblog.com/blogs/roman_rehak
"wendld" <GenieLifts@.news.postalias> wrote in message
news:D1B8082F-16ED-47B7-9FD8-C5A1EBF26DED@.microsoft.com...[vbcol=seagreen]
> This is my first attempt at pulling in the connection string from the
> machine.config. Windows Authentication is not an option since the database
> and webservers are on different domains and a DMZ is in use. I currently
> have
> the SSL encryption on, but have not encrypted the actually value in the
> config file (yet). That is the next step after I get the programs working
> this way.
> "Roman Rehak" wrote:
|||I am not using the appSettings section of the web.config. I was planning on
using the connectionstrings section of the machine.config. There will be many
applications using the same connection string and I feel it would be
appropiate to put there, I just don't know how to get to this section.
"Roman Rehak" wrote:

> in web.config:
> <appSettings>
> <add key="ConnectionString" value="Data Source=MyServer;Initial
> Catalog=AdventureWorks;User Id=MyUser;Password=MyPassword;"/>
> </appSettings>
> in the web service:
> string cnString =
> ConfigurationManager.AppSettings.Get("ConnectionSt ring");
> Roman
> --
> Roman Rehak
> http://sqlblog.com/blogs/roman_rehak
>
> "wendld" <GenieLifts@.news.postalias> wrote in message
> news:D1B8082F-16ED-47B7-9FD8-C5A1EBF26DED@.microsoft.com...
>
>
|||The code is the same whether you want to read from machine.config or
web.config. If you have the same value in both, web.config takes precedence.
Here is how you get a connection string from the connectionStrings section:
cnString =
ConfigurationManager.ConnectionStrings["Connection StringAW"].ConnectionString;
Roman
Roman Rehak
http://sqlblog.com/blogs/roman_rehak
"wendld" <GenieLifts@.news.postalias> wrote in message
news:F14C7CAD-93C8-4EA4-8358-9FC2B2E8E681@.microsoft.com...[vbcol=seagreen]
>I am not using the appSettings section of the web.config. I was planning on
> using the connectionstrings section of the machine.config. There will be
> many
> applications using the same connection string and I feel it would be
> appropiate to put there, I just don't know how to get to this section.
> "Roman Rehak" wrote:

connect from webservice

I have a webservice I am creating. I am needing to connect to a SQL server o
n
a different machine using SQL authetication. I have the connection string
located in the machine.config. Could somebody give me a sample of how to use
that connection string to connect to the database from inside a webservice.It's not any different than connecting from an ASP.NET page so if you know
how to do that, you should be all set. It's not a good practice to place
passwords in a config file so you should look into using Windows
authentication or at the very least encrypt the password in the config file.
Roman
Roman Rehak
http://sqlblog.com/blogs/roman_rehak
"wendld" <GenieLifts@.news.postalias> wrote in message
news:7A009E7B-EE22-4FFC-8B4B-FE870659D985@.microsoft.com...
>I have a webservice I am creating. I am needing to connect to a SQL server
>on
> a different machine using SQL authetication. I have the connection string
> located in the machine.config. Could somebody give me a sample of how to
> use
> that connection string to connect to the database from inside a
> webservice.|||This is my first attempt at pulling in the connection string from the
machine.config. Windows Authentication is not an option since the database
and webservers are on different domains and a DMZ is in use. I currently hav
e
the SSL encryption on, but have not encrypted the actually value in the
config file (yet). That is the next step after I get the programs working
this way.
"Roman Rehak" wrote:

> It's not any different than connecting from an ASP.NET page so if you know
> how to do that, you should be all set. It's not a good practice to place
> passwords in a config file so you should look into using Windows
> authentication or at the very least encrypt the password in the config fil
e.
> Roman
> --
> Roman Rehak
> http://sqlblog.com/blogs/roman_rehak
>
> "wendld" <GenieLifts@.news.postalias> wrote in message
> news:7A009E7B-EE22-4FFC-8B4B-FE870659D985@.microsoft.com...
>
>|||in web.config:
<appSettings>
<add key="ConnectionString" value="Data Source=MyServer;Initial
Catalog=AdventureWorks;User Id=MyUser;Password=MyPassword;"/>
</appSettings>
in the web service:
string cnString =
ConfigurationManager.AppSettings.Get("ConnectionString");
Roman
Roman Rehak
http://sqlblog.com/blogs/roman_rehak
"wendld" <GenieLifts@.news.postalias> wrote in message
news:D1B8082F-16ED-47B7-9FD8-C5A1EBF26DED@.microsoft.com...[vbcol=seagreen]
> This is my first attempt at pulling in the connection string from the
> machine.config. Windows Authentication is not an option since the database
> and webservers are on different domains and a DMZ is in use. I currently
> have
> the SSL encryption on, but have not encrypted the actually value in the
> config file (yet). That is the next step after I get the programs working
> this way.
> "Roman Rehak" wrote:
>|||I am not using the appSettings section of the web.config. I was planning on
using the connectionstrings section of the machine.config. There will be man
y
applications using the same connection string and I feel it would be
appropiate to put there, I just don't know how to get to this section.
"Roman Rehak" wrote:

> in web.config:
> <appSettings>
> <add key="ConnectionString" value="Data Source=MyServer;Initial
> Catalog=AdventureWorks;User Id=MyUser;Password=MyPassword;"/>
> </appSettings>
> in the web service:
> string cnString =
> ConfigurationManager.AppSettings.Get("ConnectionString");
> Roman
> --
> Roman Rehak
> http://sqlblog.com/blogs/roman_rehak
>
> "wendld" <GenieLifts@.news.postalias> wrote in message
> news:D1B8082F-16ED-47B7-9FD8-C5A1EBF26DED@.microsoft.com...
>
>|||The code is the same whether you want to read from machine.config or
web.config. If you have the same value in both, web.config takes precedence.
Here is how you get a connection string from the connectionStrings section:
cnString =
ConfigurationManager.ConnectionStrings["ConnectionStringAW"].ConnectionS
tring;
Roman
Roman Rehak
http://sqlblog.com/blogs/roman_rehak
"wendld" <GenieLifts@.news.postalias> wrote in message
news:F14C7CAD-93C8-4EA4-8358-9FC2B2E8E681@.microsoft.com...[vbcol=seagreen]
>I am not using the appSettings section of the web.config. I was planning on
> using the connectionstrings section of the machine.config. There will be
> many
> applications using the same connection string and I feel it would be
> appropiate to put there, I just don't know how to get to this section.
> "Roman Rehak" wrote:
>