Bulk insert unable to access file on network - c#

I have a c sharp application in one server and sql server database on different server. When I try to run bulk insert through C Sharp application, I am getting
"Cannot bulk load because the file operating system error code 5
(Access is denied)"
When i tried the same in sql server on the database server manually, it worked for windows authentication but not for sql authentication.
What should I do in order to make sql authentication work. Please help.

The SQL Server uses security profile of SQL Server service account to access the file if SQL Server authentication was used. OTOH with windows authenticatin, the authenticated windows account must have acces to the file. That's why it worked for you in one case and not in the other one. Give SQL Server service account access to the file.
The other point here is delegation. See this article for the explanation (and a remedy):
http://blogs.msdn.com/b/dataaccesstechnologies/archive/2010/10/29/sql-bulk-copy-error-operating-system-error-code-5-access-is-denied.aspx

To resolve this error, use SQL Server Authentication and specify a SQL Server login that uses the security profile of the SQL Server process account, or configure Windows to enable security account delegation.
Plese go through these references also BulkInsert ,SecurityAccountDelegation
Hope this will help.

Related

SQL Express Connection Error When Connecting to Database

I've been trying to connect my Visual basic to SQL because this is what we are taught off but when I execute it, it shows this. How do I fix this?
This Windows user needs to be granted access to SQL Server and that specific database. It is usually configured during installation process of SQL Server. You can still do it by opening SSMS and adding this user under Security -> Logins node from Object Explorer panel.
Of course, to be able to do above, you need to connect to your SQL Server Express first. You may need to use another Windows user or a SQL Server user. The latter would be possible only if SQL Server Authentication has been enabled during installation process by choosing Mixed Mode for authentication (Windows + SQL Server).
You need to get full permission in SQL Server.
Open SSMS there is a Security folder then => Logins.
In Logins you will find your PC's username which is connected to Sql service
Right-click on your username => Properties
Check Windows Authentication Mode

Authentication: Microsoft SQL Server Database File (SqlClient)

I have been using "(LocalDB)\MSSQLLocalDB" as my database, and because I don't know SQL commands, I used "Linq to SQL Classes" to connect to the database and tables.
Now its the problem, I'm just using "Linq to SQL" to connect to the database and the "(LocalDB)\MSSQLLocalDB" authentication is set to "Windows Authentication", and because of that, I cannot run the application on another PC. it will give me this error:
Cannot open database "C:\USERS\******.MDF" requested by the login. The login failed.
Login failed for user 'blah\blah'
I tried to change the authentication mode to the "SQL Server Authentication" but it won't accept any username password and will show this error:
The attempt to attach to the database failed with the following information: login failed for user 'blah'.
What should I do to run my application on other PCs without worrying about the authentication, Can I just remove the authentication completely?
you just have to ensure that the windows accounts on both machines are members of the same group, and that group has the appropriate permissions. For example, if both accounts are Local Administrators, then you can use that group
Instead of "(LocalDb)" use the actual machine name of your computer. It looks from the error message that on the other computer it is looking for a copy of the local SQL Server engine but not finding it.
Using the server name instead of LocalDb will make it look for the other computer's copy of SQL Server and connect there.

Why can't I connect to the SQL Server database when I create setup file in Visual studio?

I create application using C# windows forms which uses local SQL Server database server to store / read data using this connection string:
SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=mydb;Integrated Security=True");
After that I created a setup file (exe) then I installed the setup file in same PC and the application works well and can read data from the SQL Server database.
Now the problem came when I went to another PC I installed SQL Server and I manually attached the same database (mydb) and installed my application and it did not work. The error is shown in the screenshot:
I have different SQL Server name in the other PC but I specified in the connection string that the SQL Server is Localhost so I don't know why it didn't work. Please help me how to fix this error. Thank you
Make sure Sql Server is set to allow Tcp/Ip connections and that it's set to allow integrated security logins. Then, make sure the user on that machine is granted the correct access to the database. You may have better luck using Sql authentication over Windows Authentication here, if you want to be able to distribute this application and have it "just work".
Finally, if Sql Server here is meant to be a simple datastore for a typical local desktop application, it's probably overkill. Full Sql Server (including Express Edition) is a server engine. It works best when it's the only thing running on the machine and can use up all of the resources on the machine in order to cache data and handle requests from many remote machines. If you just want a local data store for a typical desktop application, an in-process engine like Sql Server LocalDb, Sql Server Compact Edition, Sqlite, or even MS Access would be a much more appropriate choice.
You need to set up Windows authentication for the user that's logged in on the other computer. Because you are using Integrated Security, it will attempt to connect to SQL Server using the windows login of the current user. This will be a different login on different computers, so the SQL Server on the other computer needs to have that user added.
You need to start SQL Server Browser Service. It lets your app connects to SQL Server:
SQL Server Browser listens for incoming requests for Microsoft SQL
Server resources and provides information about SQL Server instances
installed on the computer.
To enable it:
In SQL Server Configuration Manager, go to Properties => Service tab => Start Mode = Automatic.
Or
This computer => Manage => Services and Applications => Services => SQL Server Broswer
Note: You need a sql specific user for connect to it, you can not go with Integrated Security for every machine.
Create a Sql Server user and allow it to access the Database:
--//Creates the login AbolrousHazem with password '340$Uuxwp7Mcxo7Khy'.
CREATE LOGIN AbolrousHazem
WITH PASSWORD = '340$Uuxwp7Mcxo7Khy';
GO
--//Creates a database user for the login created above.
CREATE USER AbolrousHazem FOR LOGIN AbolrousHazem;
GO
Reference

SQL server login error remote server database

I am connecting to two remote databases (sql server and the databases are on the same remote machine). Both databases are in separate directories.
I checked the security settings in the SQL management console for both databases and they are the same. However, I can connect to one database remotely (using a remote connection string in my C# application) but the other database gives me "Login failed - Login failed for user XXX/ZZZ".
Any ideas why this is happening? (Could it be related to the permissions on the physical directories where the files of the database are located)?
In SQL management console, check both Logins on the server and Users on each database.
It is a login issue as stated by the error. Make sure you can get to both databases uses SSMS using the logins that the C# application is using. If this works, then it is something else.
One common problem is not changing the default database from [master] or adding the user with rights to the database you want to query.

Poor Performance when logging into SQL Server using Active Directory in Win Forms C#

My application connects to a SQL Server using windows authentication. I have a login in on the server which is a Active Directory group. If the user is in the Active Directory group then it logs you into the SQL server and gives you the correct permissions.
However this way is much slower than using SQL Server authentication, is there any way to increase the performance.
I did some tests for how long it takes to login into the server:
SQL Server Authentication - 0.068414 s
Windows Authentication using AD - 0.182627 s
Thanks
I wouldn't be worried about this. All MS technologies for connecting to database have a database connection pool will be kept in the background so there won't be many opening and closing of connections.
0.18267s is most likely nothing compared to actual queries you run on the server.
If you really want to look into this some more you can try following:
make sure AD is not too busy
make sure there is good connectivity between SQL Server and AD (use Ping to verify)
if possible give permissions to AD users vs AD group.
I was just going to add a comment, but it got long. Now its here.
Anyhew. When you provide credentials to SQL Server directly it can perform its own authentication and return.
However, when you specify AD credentials SQL Server then has to do another round trip to the Domain Controller to confirm your credentials (although technically I think you are actually passing a token. Anyway).
My 2c.

Categories

Resources