I am trying to use the SqlDataConnection type provider to connect to a SQL database on Azure.
type dbSchema = SqlDataConnection<"Server=tcp:123456.database.windows.net,1433;Database={database};User ID={user};Password={password};Trusted_Connection=False;Encrypt=True;Connection Timeout=30;">
When I try to connect to the database with the associated connection string I get the following error:
Invalid object name 'syscomments'.
I cannot seem to find a solution to this problem other that using a different method to connect to the database (such as FSharp.Data.SqlClient). I believe this is related to the Azure implementation of Sql Server however I am unable to find any information on how to solve this problem. Any help is much appreciated.
I was having the same problem with the Microsoft Sql Type Provider. I started using this Sql Type Provider to better results: http://fsprojects.github.io/SQLProvider/
Thanks to Ross McKinley for pointing me to it.
Related
I'm trying to set up a Debezium SQL Server Connector against a SQL Server instance that is controlled by DBAs at my workplace. I've been able to start up Zookeeper and Kafka Server without issue, and Kafka Connect itself works with sample Connectors, but when attempting to start a Debezium SQL Server Connector instance I've been getting the error "Couldn't obtain database name".
[2022-07-12 16:36:04,269] ERROR Stopping after connector error (org.apache.kafka.connect.cli.ConnectStandalone:117)
java.util.concurrent.ExecutionException: org.apache.kafka.connect.runtime.rest.errors.BadRequestException: Connector configuration is invalid and contains the following 1 error(s):
Unable to connect. Check this and other connection properties. Error: Couldn't obtain database name
Here is my debezium config:
name=Dbz-SqlServer-connector
connector.class=io.debezium.connector.sqlserver.SqlServerConnector
database.hostname=MyDbHost
database.port=1433
database.user=MyUsername
database.password=MyPassword
database.dbname=MyDatabase
database.server.name=MyDbHost
table.include.list=dbo.CdcTest
database.history.kafka.bootstrap.servers=localhost:9092
database.history.kafka.topic=dbhistory.CdcTest
I've tried this in a .properties file passed to a standalone Connect instance, and as a JSON POST to a distributed Connect instance. I have tried all of the same steps on both my local Windows machine as well as on a linux VM, with the same results.
Confluent and Docker are not options for me in this situation.
for SQL Server login credentials, I am using a local account on the SQL Server instance that does have access to the database in question. I found the source code for debezium's connectors on their github and was able to find that specific error message within the code:
private static final String GET_DATABASE_NAME = "SELECT name FROM sys.databases WHERE name = ?";
...
public String retrieveRealDatabaseName(String databaseName) {
try {
return prepareQueryAndMap(GET_DATABASE_NAME,
ps -> ps.setString(1, databaseName),
singleResultMapper(rs -> rs.getString(1), "Could not retrieve exactly one database name"));
}
catch (SQLException e) {
throw new RuntimeException("Couldn't obtain database name", e);
}
}
I'm not completely familiar with Java but it appears that basically something is going wrong when the connector is trying to run "SELECT name FROM sys.databases WHERE name = 'MyDatabase'". When I run this against the database myself, logged in with the same account I'm using, it seems to work just fine, so I'm really not sure where to go from here. It is fair to say that since I'm not in full control of the SQL Server environment that I'm using, there may be some permissions issues that I'm not aware of, but from what I'm able to test it seems like it should be working.
I would greatly appreciate any help at all, whether just suggestions on settings/configs to check or a full-blown solution.
Thank you!
Update: I've built a simple console app to run that sys.databases query against MyDbHost, master database, as the relevant account, and it's working just fine so I feel like that confirms that my connection info is correct and account permissions are also correct. Seems like this is an issue within the Debezium connector.
It turned out that my problem was a mistake in the connector's config setting. I misunderstood which specific pieces of data to put into database.hostname and database.server.name, and one I corrected those fields the connector works.
Apparently, using AttachDbFilename and user instance in your connection string is a bad way to connect to a DB. I'm using SQL server express on my local machine and it all seems to work fine. But what's the proper way to connect to SQL server then?
Thanks for your explanation.
Using User Instance means that SQL Server is creating a special copy of that database file for use by your program. If you have two different programs using that same connection string, they get two entirely different copies of the database. This leads to a lot of confusion, as people will test updating data with their program, then connect to a different copy of their database in Management Studio, and complain that their update isn't working. This sends them through a flawed series of wild goose chase steps trying to troubleshoot the wrong problem.
This article goes into more depth about how to use this feature, but heed the very first note: the User Instance feature has been deprecated. In SQL Server 2012, the preferred alternatives are (in this order, IMHO):
Create or attach your database to a real instance of SQL Server. Your connection string will then just need to specify the instance name, the database name, and credentials. There will be no mixup as Management Studio, Visual Studio and your program(s) will all be connecting to a single copy of the database.
Use a container for local development. Here's a great starter video by Anna Hoffman and Anthony Nocentino, and I have some other resources here, here, and here. If you're on an M1 Mac, you won't be able to use a full-blown SQL Server instance, but you can use Azure SQL Edge if you can get by with most SQL Server functionality (the omissions are enumerated here).
Use SqlLocalDb for local development. I believe I pointed you to this article yesterday: "Getting Started with SQL Server 2012 Express LocalDB."
Use SQL Server Compact. I like this option the least because the functionality and syntax is not the same - so it's not necessarily going to provide you with all the functionality you're ultimately going to want to deploy. Compact Edition is also deprecated, so there's that.
Of course if you are using a version < SQL Server 2012, SqlLocalDb is not an option - so you should be creating a real database and using that consistently. I only mention the Compact option for completeness - I think that can be almost as bad an idea as using AttachDbFileName.
EDIT: I've blogged about this here:
Bad Habits : Using AttachDBFileName
In case someone had the problem.
When attaching the database with a connection string containing AttachDBFile
with SQLEXPRESS, I noticed this connection was exclusive to the ASP.NET application that was using the database. The connection did block the access to all other processes on the file level when made with System.Data.SqlClient as provider.
In order to assure the connection to be shareable with other processes
instead use DataBase to specify the database name in your connection string
Example or connection string :
Data Source=.\SQLEXPRESS;DataBase=PlaCliGen;User ID=XXX;password=ZZZ; Connect Timeout=30
,where PlaCliGen is the name (or logical name) by which SQLEXPRESS server knows the database.
By connecting to the data base with AttachDBFile giving the path to the .mdf file
(namely : replacing DataBase = PlacliGen by AttachDBFile = c:\vs\placligen\app_data\placligen.mdf) the File was connected exclusively and no other process could connect to the database.
I am facing some issues to make the connection to the particular schema on SQL Server because the setschema method is deprecated in JDBC API. So please help to get any alternative.
I got following error:
o.h.engine.jdbc.spi.SqlExceptionHelper : Warning: setSchema is a no-op in this driver version
I'm pretty sure you can't set a schema for a sql-server session. What you can do is define a user with a default schema of the one you want.
I’m trying to query our database in F# but I’m having a lot of errors when I get the data context.
Here is my connection command:
#r "System.Data.dll"
#r "FSharp.Data.TypeProviders.dll"
#r "System.Data.Linq.dll"
open System
open System.Data
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq
type dbSchema = SqlDataConnection<"Data Source=OCPM;Initial Catalog=Nautilus;Integrated Security=SSPI;">
let db = dbSchema.GetDataContext()
147 errors in total are returned after this simple script, I won’t list them all but here are a couple of examples:
Script1.fsx(11,17): error FS3033: The type provider
'Microsoft.FSharp.Data.TypeProviders.DesignTime.DataProviders'
reported an error: Error reading schema. Warning : SQM1025: Unable to
extract stored procedure 'utvf_VehiclesSummaryUDT' from SqlServer because parameter 'incident' is the unsupported DbType 'Structured'.
Script1.fsx(11,17): error FS3033: The type provider
'Microsoft.FSharp.Data.TypeProviders.DesignTime.DataProviders'
reported an error: Error reading schema. Warning : SQM1014: Unable to
extract stored procedure 'asynch.usp_LBNGBasket' from SqlServer.
Invalid object name '#updatedPriceTab'.
I believe this is due to the complexity of the objects and structures used in our DB. The first error is due to a User-Defined Table Types called udt_IncidentExposure in the function utvf_VehiclesSummaryUDT. The second is because the temporary table #updatedPriceTab is called in asynch.usp_LBNGBasket but created in another stored procedures.
I’m surprised of the limited compatibility between F# and SQL Server on complex structures. Is there any other way to connect to SQL server in F#? All I need is doing very simples SQL queries.
Additional information: SQL Server version in use is 10.50.4270
You are out of luck with using production-quality SqlClient type provider because it requires at least SQL2012 to operate.
You may try SQL Type Provider, although I do not have any experience using it in production scenarios.
Also you may try using SqlEntityConnection type provider that sometimes does better job, than SqlDataConnection type provider that you've tried.
And finally, as the last resort, nothing prevents you from accessing SQL from F# through plain ADO.NET.
You can try other F# type providers from here:
http://fsharp.org/guides/data-access/#sql-data-access
The best one (IMO) for MS SQL Server is FSharp.Data.SqlClient but I believe it supports only version 2012 and up.
I have some Crystal Reports connecting to a Sql Server db that I would like to detect whether the connection is trusted or whether I need to supply the log on info (reports are not supplied by me so I can't control the connect method). If I just blindly supply login credentials, it won't connect if it is a trusted connection.
The following does not work:
oRpt = oCR.OpenReport("C:\MyReport.rpt")
if oRpt.Database.Tables(1).ConnectionProperties.Item("Integrated Security") = True then
'trusted connection
else
'supply login credentials
end if
It gives the following error:
Operator '=' is not defined for type 'IConnectionProperty' and type 'Boolean'.
I cannot find how create a construct in vb.net for IConnectionProperty. I can't find any documents from Crystal that explain it.
I am using Crystal Reports XI - Developer
I think i found the answer. By using the property ConnectBufferString
Like this:
Console.WriteLine(oRpt.Database.Tables(1).ConnectBufferString.ToString)
It will give you a string like this
Provider=SQLOLEDB;;Data Source=MYPC\SQLEXPRESS;;Initial Catalog=sample_db;;User ID=;;Password=;;Integrated Security=-1;;Use DSN Default Properties=0;;Locale Identifier=1033;;Connect Timeout=15;;General Timeout=0;;OLE DB Services=-5;;Current Language=;;Initial File Name=;;Use Encryption for Data=0;;Replication server name connect option=;;Tag with column collation when possible=0
You just look for the following:
Integrated Security=-1 = Trusted Connection
Integrated Security=0 = Untrusted Connection
Hope this helps someone else since I wasted a few hours looking.