ArgumentException was unhandled by user code: (System.ArguementException) occurred in System. Data.dll - sql-server

I was following an online Tutorial and i am very new to programming.
Additional information: Keyword not supported: 'server: xxxxx;database
Here's my Code:
Dim sqlCon As New SqlConnection With {
.ConnectionString = " Server= xxxx;Database = ist2gx;User= Ist2gx;Password=hnu91p;Trusted_Connection= True;ProviderName=SqlClient"
}
Please can someone help me out.

ConnectionString does not have a property named ProviderName in it. ProviderName is different from provider property in ConnectionStrings string.
You should change ProviderName=SqlClient with Provider=sqloledb for OLEDB connection strings.
Sql ConnectionString for a standard connection with username and password:
`"Data Source=ServerName; Initial Catalog=DataBaseName; User id=UserName; Password=Secret;Provider=sqloledb"`
Sql ConnectionString for a trusted connection:
`"Data Source=ServerName; Initial Catalog=DataBaseName; Integrated Security=SSPI;Provider=sqloledb"`
For more info take a look at Connection Strings for different providers and SQL Server connection strings.

Related

SQL Server connection string in Appsettings.json + .net Core 3.1

I'm trying to setup and connect to my remote development SQL Server (SQL 2017) in appsettings.json within .NET Core 3.1, I have tried the following approaches yet without any success.
"ConnectionStrings": {
//"DefaultConnection1": "DATA SOURCE=[servername];UID=[username];PWD=[password];DATABASE=[databasename]",
//"DefaultConnection2": "Data Source=[servername]; Initial Catalog=[databasename];User ID=[username];Password=[password]",
//"DefaultConnection3": "DataSource=[servername];Initial Catalog=[databasename];User Id=[username];password=[password]",
"DefaultConnection4": "Server=[servername];Database=[databasename];User Id=[username];password=[password];Trusted_Connection=False;MultipleActiveResultSets=true;"
},
Error of DefaultConnection1:
ArgumentException: Keyword not supported: 'uid'.
Microsoft.Data.Sqlite.SqliteConnectionStringBuilder.GetIndex(string
keyword))
Error of DefaultConnection2:
ArgumentException: Keyword not supported: 'initial catalog'.
Microsoft.Data.Sqlite.SqliteConnectionStringBuilder.GetIndex(string
keyword)
Error of DefaultConnection3:
ArgumentException: Keyword not supported: 'initial catalog'.
Microsoft.Data.Sqlite.SqliteConnectionStringBuilder.GetIndex(string
keyword)
Error of DefaultConnection4:
ArgumentException: Keyword not supported: 'server'.
Microsoft.Data.Sqlite.SqliteConnectionStringBuilder.GetIndex(string
keyword)
Any ideas, pointers or help is much appreciated.
thanks for your help and advice, i might have just found the solution and am able to connection the SQL database successfully now, the problem was as following:
The following package was installed on the solution:
Microsoft.Data.Sqlite.SqliteConnectionStringBuilder
in Startup.cs the following line was used in the ConfigureServices(IServiceCollection services):
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlLite(Configuration.GetConnectionString("DefaultConnection")));
However after research i found out that i should rather use the following line:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Which was not accessible due the following package missing:
Microsoft.EntityFrameworkCore.SqlServer
After installing this package via package Manager using the following command:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
I was able to access the SQL server using the following connection string:
"ConnectionStrings": {
"DefaultConnection": "Server=[servername];Database=[databasename];Persist Security Info=True;User ID=[username];Password=[password];MultipleActiveResultSets=True;"
},
For safety, as Christian pointed out, I removed also the other connectionstring references.
Probably it's just because you're not supposed to put comments (//) in JSON as JSON is a data format and doesn't support comments. So try actually removing the lines starting with // from your config.
Your setup in appsettings.json is fine.
Suppose the model in your cs file is mytestModel.
Here is the code to read the connection string:
public class mytestModel : PageModel
{
public string connectstring = "";
private IConfiguration MyConfiguration;
public mytestModel (IConfiguration _configuration)
{
MyConfiguration = _configuration;
}
public void OnGet()
{
connectstring = this.MyConfiguration.GetConnectionString("DefaultConnection4");
}
}
It should be noted that the type of model (eg. PageModel) does not matter. The name of the model is what matters.

How to Dapper & AseClient

Unable to connect to Sybase, not getting error.
I have referenced Sybase.AdoNet4.AseClient.dll from C:\Sybase\DataAccess\ADONET\dll to my MVC 5 project, running in .NET 4.5
Web.Config Connections string
<add name="MyASEServer" connectionString="Data Source=127.0.0.20:12000;Initial Catalog=MyDB;User Id=USer1;Password=Password1;"
providerName="Sybase.Data.AseClient"/>
My code
string constr = ConfigurationManager.ConnectionStrings["MyASEServer"].ConnectionString;
IDbConnection aseDB = new AseConnection(constr );
var myCustomer = aseDB.Query<Customer>("select * fromdbo.customer");
When the debugger reaches aseDB.Query...., it never return and not receiving any error.
What am I missing.
In this case, Application Insights is on in visual studio and all the exceptions are captured in it. So the application screen is not showing the exception.

ASP.NET Web Service Cannot Access SQL Server DB

I created a Web Service project in VS 2010. I can get the non SQL functions to work perfectly. But I cannot get a simple Sql Server DB connection to even open. The connect string works fine for other ASP.NET web sites I have created. I am currently just running it locally on my development box. But the other ASP.NET Web Sites work locally fine with the same connect string. My code is below. The return on the error catch is also below. I have search google and find nothing to assist me in fixing this issue. Any help would be gratly appreciated. Thanks!!
System.Security.SecurityException: Request for the permission of type 'System.Data.SqlClient.SqlClientPermission, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) at System.Security.PermissionSet.Demand() at System.Data.Common.DbConnectionOptions.DemandPermission() at System.Data.SqlClient.SqlConnection.PermissionDemand() at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at WebServiceTest.Service1.GetAccountName(String VID) The action that failed was: Demand The type of the first permission that failed was: System.Data.SqlClient.SqlClientPermission The Zone of the assembly that failed was: Intranet
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
<WebMethod()> _
Public Function ThisIsATest() As String
Return "This Is A Test"
End Function
<WebMethod()> _
Public Function GetAccountName(VID As String) As String
Dim conn As New SqlConnection
Dim myc As New SqlCommand
Dim wk As String
wk = ""
conn.ConnectionString = "Data Source=xxxxxxxx;Initial Catalog=WORKT;Integrated Security=False;UID=readonly;PWD=readonly"
Try
conn.Open()
Catch e As Exception
wk = e.ToString
End Try
conn.Close()
Return wk
End Function
For anyone else searching this issue, It ended up being a permission issue because my app was stored on a network share. Here is the web site that helped me fix it. I had to add the shared mapped drive to the ASP trusted folders.
http://msdn.microsoft.com/en-us/library/zdc263t0(v=vs.90).aspx

How to use MSSQL DSN as URL in DataSource.groovy

I have been trying this code in DataSource.groovy but its showing an error of "No suitable driver"
dataSource
{
pooled = true
driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
String url="jdbc:microsoft:sqlserver:DSNname";
}
I have already included two jdbc SQLserver drivers sqljdbc, sqljdbc4 (*.jar files).
I also tried this code but went futile.
DataSource
{
driverClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
//url="jdbc:sqlserver://localhost:1433;database=DBNAME" its working
url="jdbc:odbc:myDSN"
username="sa"
password=""
}
Thanks a ton in advance.

What's the difference between app.config's connection string and c# connection string

Is there any difference between the following two lines of code in c# and app.config file.
C# connectionString declaration.
string conn = "/server = test/ DB = test_dev/ env = dev"
and
app.config declartion
<connectionStrings>
<add name="Test" connectionString="Data Source=.;Initial Catalog=test_dev;" providerName="System.Data.SqlClient" />
</connectionStrings>
How can I declare c# connection string to the format in the app.config file so that I can read from the app.config file.
There's no real difference between hard coding a connection string and pulling one out of the app.config file.
The advantage of using app.config is that you can use that same connection string in multiple places in your application, and then if you need to change it (for testing purposes or anything else really), you only have to change it in one spot that is nicely contained in a configuration file.
As mentioned in the comments, to read a connection string directly from your app.config, you can use this:
string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Test"];
although there are many different ways to access the connection string (DataSet, etc.)
To read connectionString values in your C# code, you can access System.Configuration.ConnectionStringSettingsCollection
System.Configuration.ConfigurationManager.ConnectionStrings
will return collection of all connection strings defined in <connectionStrings></connectionStrings> section
You can access connection strings by name using
string conn =
System.Configuration.ConfigurationManager.ConnectionStrings["Test"];

Resources