What should be the Connection String of my project? - asp.net

I have hosted a new website from www.hostingfarms.com. They have a file manager for uploading the website content and backup / restore DB (.bak) of MS SQL. I uploaded it. But when I log in into my website (www.theyuvaworld.com), database not gets connected. I have used this connection string in my local PC
SqlConnection con = new SqlConnection("server=.;database=test;integrated security=true");
SqlCommand cmd = new SqlCommand();
Should I make any changes here.
Note : I have created a user in file manager's database i.e Username = Nikhil and Password = **.
I will give any other details if wanted.
Regards,
Nikhil

I figured out this,
Connection string syntax is
data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

Related

How to retrieve a docx file from SQL Server database to ASP.NET Core console application?

I am making a micro-application using an ASP.NET Core console application and I need to retrieve a .docx file from a server database.
Any examples or suggestions?
I would recommend using the new .Net Core 3.1 Windows Service template. I recently wrote 3 small services that update database tables from other data sources. One being LDAP and the other being an Excel file available on a SFTP server.
Depending on your Use Case, I would recommend either using a connection to the SQL server using the SqlConnection class, and access the document from there, or access the document using SFTP (database server or not). There are some libraries for SFTP for .Net Core and plenty of tutorials online.
In either case, you would use your preferred connection to download the file locally as some sort of data stream most likely. Once it's downloaded locally to the server you could further utilize SFTP to send the file wherever you want on the network.
Question: Why are you downloading a docx file from a "Database" server? Is the docx file just "living" on a server that also happens to host databases on it?
Just use SqlClient to read the data as a Stream and CopyTo a FileStream. EG
var fn = "Seat Assembly.doc";
var outFile = $#"c:\\temp\\{fn}";
using (var con = new SqlConnection("server=localhost;database=AdventureWorks2017;Integrated Security=true"))
{
con.Open();
var cmd = con.CreateCommand();
cmd.CommandText = #"
select Document
from production.document
where FileName = #fn";
cmd.Parameters.Add(new SqlParameter("#fn", fn));
using (var rdr = cmd.ExecuteReader())
using (var fs = File.OpenWrite(outFile))
{
if (!rdr.Read())
{
throw new InvalidOperationException($"File not found {fn}");
}
rdr.GetStream(0).CopyTo(fs);
fs.Close();
}
}

ConnectionString is incorrect in axapta

I want to connect to the other database using LoginProperty class. I did it in two different ways:
Create a DNS system object, using loginProperty class and connect to the DB. This way it works fine.
In the other way, i wants to creat a 'Connection String' and connect to the DB. In this way i am getting an error.
ODBC operation failed. Unable to logon to the database.
[Microsoft][ODBC Driver Manager] Data source name not found and there is no standard driver.
My Connection string is:
connectionString = "Driver={SQL Server Native Client 10.0}; Server=MSS2008-term\\\\SQLServer; Database=db; user ID=sa; Password=ABC.123";
loginProperty.setOther(connectionString);
conn = new ODBCConnection(loginProperty);
What is missing in the connection string?
Try using a raw string assignment (so no \ magic):
connectionString = #"Driver={SQL Server Native Client 10.0}; Server=MSS2008-term\SQLServer; Database=db; user ID=sa; Password=ABC.123";
Some others have problems, see ODBC connection to external data.
Maybe you can create the DSN by code, see What is the simplest, most maintainable way to create a SQL Server ODBC Data Source?
I notice you have Server and Database in your connection string where I use Data Source and Initial Catalog for some reason. You can do it like this, just changing/adding relevant parameters:
public str getConnectionString() {
System.Data.SqlClient.SqlConnectionStringBuilder dbConnectionStringBuilder;
dbConnectionStringBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();
dbConnectionStringBuilder.set_Item('Data Source', 'YourServer');
dbConnectionStringBuilder.set_Item('Initial Catalog', 'YourDatabase');
dbConnectionStringBuilder.set_Item('Integrated Security', true);
return dbConnectionStringBuilder.ToString();
}

Connecting with ms acess database on ftp server using oledb connection string

Could You tell me how can I connect with my ms access database on my ftp server ?. I'am using this connection string:
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "provider=microsoft.jet.oledb.4.0; data source=ftp://ftp.myAccount.pl/myAccount.pl/dataBase/file.mdb; User Id=myUserName; Password=myPassword";
con.Open();
but wont work :(.
Here's a working example: http://www.zbconsulting.eu/?tabid=58
Contains a working demo too.

Why is this SQL connection in my ASP.NET page failing?

I have the following code
SqlConnection conn = new SqlConnection("user id=clearpath\\user;" +
"password=Password1;server=sqldatamart;" +
"Trusted_Connection=yes;" +
"database=LENDER_LOAN_SERVICE;" +
"connection timeout=30");
SqlCommand cmd = new SqlCommand("INSERT INTO [LENDER_LOAN_SERVICE].[CMC].[Turn_Times] values('"+dateadded+"','"+username+"','"+prevtime+"','"+type+"','"+newturntime+"')",conn);
conn.Open();
which works fine when I am doing local development. However when I put the website on my IIS 6 on my computer I get the following error.
Login failed for user 'CLEARPATH\IT-CARLOSDELL$'.
Line 42: SqlCommand cmd = new SqlCommand("CMC.sp_NewDocMod", conn);
Line 43:
Line 44: conn.Open();
Line 45:
Line 46: SqlDataReader reader = cmd.ExecuteReader();
with an error on line 44.
It seems its changing the connection string from clearpath\cpereyra to clearpath\IT-CARLOSDELL$
My computer name is IT-CARLOSDELL
any ideas on how to fix this?
Thanks
Sounds like your application pool is using 'LocalSystem' as pool identity.
You need to do one of the following:
Give permissions in SQL for the CLEARPATH\IT-CARLOSDELL$ account
Change the identity on the application pool to one with permissions in the DB (Perhaps clearpath\cpereyra)
Set up authentication on your site to impersonate the user who connected to the site and has rights in the SQL DB.
Remove Trusted_Connection=yes from your connection string.
Which one you pick depends on the approach you use to secure your site.
BTW: It is not changing the connection string. It is using that account because you have Trusted_Connection=yes. It is using the account the ASP.NET worker process is running under to try to connect to the database.
Remove the Trusted_Connection=yes from your connection string. Right now, the string is giving a specific user name and password and also saying to use the pass-through Windows authentication. When running locally, your Windows account has rights to SQL (I assume), but in IIS, it is most likely logging on as anonymous.

What's wrong with this ASP connection string?

I'm at the end of my rope on this. It should be so simple.
I just need to know what's wrong with this connection string:
dbc.open ("Driver={SQL Server}; Data Source = ServerName; Initial Catalog = InitialDB; " "User ID = Username; Password = Password;")
I get this error when running that line:
[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
I know ServerName is up and accepting connections, I know InitialDB exists, I know User ID and Password are valid for the database. What am I missing?
In notepad create file anyname.udl - let it be empty. After in explorer click it - you will get a dialog to create OLEDB connection string, select expected driver, and all connection param, ensure that "Allow saving password" = True. Press Ok.
Then again open file with notepad. Content is valid connection string
Try this...
dbc.open ("Provider=SQLOLEDB; Driver={SQL Server}; Data Source = ServerName; Initial Catalog = InitialDB; User ID = Username; Password = Password; Network Library=dbmssocn;")
As someone has already pointed out, udl is the best easiest way to create a conn string - here is a link that talks about it. https://web.archive.org/web/20210211044624/http://www.4guysfromrolla.com/webtech/070400-1.shtml
If you are using ADOdb you might want to try
"Provider=SQLNCLI10;Server=SERVER;Database=DATABASE;Uid=USERNAME;Pwd=PASSWORD"
for SQL Server 2008 Native Client or
"Provider=SQLNCLI;Server=SERVER;Database=DATABASE;Uid=USERNAME;Pwd=PASSWORD"
for SQL Server 2005 Native Client.
For ODBC, use
"Driver=SQL Server Native Client 10.0"
for SQL Server 2008 Native Client or
"Driver=SQL Native Client"
for SQL Server 2005 Native Client.
What's with the " " in the middle of the string?
Your connection string appears to be mixing ODBC and OLEDB. I would suggest visiting http://www.connectionstrings.com/ and finding the correct syntax for the desired provider.
Yours:
"Driver={SQL Server}; Data Source = ServerName; Initial Catalog = InitialDB; " "User ID = Username; Password = Password;"
ODBC:
"Driver={SQL Server};Server=ServerName;Database=InitialDB;Uid=Username;Pwd=Password;"
OLEDB:
"Provider=sqloledb;Data Source=ServerName;Initial Catalog=InitialDB;User Id=Username;Password=Password;"
Do you have visual studio?
Connect to the database server, and locate the database you want to connect to.
Right click, select properties. Your connection string to the db is right there.
Copy to wherever you want. -- Should be in web config, but you can paste it directly into code if you so desire.

Resources