vsts extension making database calls - sql-server

I'm investigating building a VSTS Extension that can call an external SQL database passing a task ID, display the results in a task, and pass back an update if needed back to the SQL database. I have been looking for a while for a microsoft help doc that will answer this question but have not had any luck. Is it possible to interact with an external SQL database using a VSTS Extension? If so, is there any documentation out there that I missed?

There isn’t the built-in api to do with database in VSTS extension, the VSTS extension uses Data storage to store data.
Anyway, you just need to do it directly with some libraries, for example:
var connection = new ActiveXObject("ADODB.Connection") ;
var connectionstring="Data Source=<server>;Initial Catalog=<catalog>;User ID=<user>;Password=<password>;Provider=SQLOLEDB";
connection.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT * FROM table", connection);
rs.MoveFirst
while(!rs.eof)
{
document.write(rs.fields(1));
rs.movenext;
}
rs.close;
connection.close;
Related thread: How to connect to SQL Server database from JavaScript in the browser?
The better way is that you can build a API project (e.g. web api) to do with database (need to be accessible from internet), then you can call that API with necessary data in you VSTS extension.

Related

Should we delete physical database for the deleted clients in SAAS application

I am working on a project which is in .net core and hosted on azure and being used as SAAS application by different clients all over the world having separate databases.
Now I got a requirement which says we should delete the databases of those clients which are no longer in master db and deleted by admin.
I am in doubt that should we really delete the database for the clients which would be getting deleted?
Also if yes is it feasible to delete the database through application or we should use some utility to do that?
Also I am afraid if server will allow application to delete the database?
Can any one please suggest me on this?
Following tech stack i am using:
.net core (backend) with EF core
MSSQL (database)
Theres a few different ways.. Essentially it works the same as any other database, so you can execute T-SQL to do it.. or use the Azure SDK.. Here is some examples of different approaches: https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-database-transact-sql?view=sql-server-2017
You can execute raw SQL statements in EF like so:
using(var context = new SampleContext())
{
var commandText = "INSERT Categories (CategoryName) VALUES (#CategoryName)";
var name = new SqlParameter("#CategoryName", "Test");
context.Database.ExecuteSqlCommand(commandText, name);
}
So, using that format, you could add your DROP Database command in? Taken from: https://www.learnentityframeworkcore.com/raw-sql - under the Database.ExecuteCommand section

file share error if Sql Compact 4 in windows application

I am sorry but I want a final answer about that.
First I used SQL compact 3.5 for long time and it take from me long time to make tables and it is work good per one user application but now I have customer who want to run my soft on 8 computer by local network so I said ok then I try to share data file .sdf on server and use it but I get an error I don't remember it so I am searched on the internet and I saw that I must update to SQL compact 4 so update it and after that I get this Stupid error
I can’t Believe that this data don't support this type of use and it take too long time to make tables and other things on SQL compact database and the my customer will not wait me so what is the Reason
My SQL statement is this:
dt = New SqlCeConnection("Data Source=" & dpaa & "\MoveData.sdf;Encrypt Database=True;Password=123cdswdaas;File Mode=Read Write;Persist Security Info=False")
I think that previous answer is slightly misleading - you are definitely able use SQL Server CE 4.0 with database file located on network share. In fact, I am using this functionality in one of my active projects right now. "File Mode=Exclusive" parameter is not necessary - exclusive mode is the only available mode in such case.
The major drawback of this approach is that only one client is able to read from or write to database file at a given time, due to exclusive locking of entire SDF file. But there a circumstances when it is not possible to have full-featured SQL server in your environment (domain policy etc.). In such case shared database file is the only solution available.
Accessing SQL Compact database .sdf file on a network share by multiple users is not supported. You should use SQL Server Express edition for this. There are also multiple posts on stackoverflow on this subject. The version 3.5 supports opening .sdf file exclusively from a network share but 4.0 does not. But no SQL CE version supports shared access of multiple network users to 1 .sdf file.
But upgrading your application to support both SQL Express and SQL Compact databases could be relatively easy task. It depends on how your application access data. For example using Entity Framework your queries could be generated depending on your actual database connection.
You can also use generic classes DbConnection, DbCommand etc. instead of SqlCeConnection, SqlCeCommand etc. - thus you can change used database type without having to maintain two separate versions of your project.
Download SQL Express 2014 with Tools SQLEXPRWT http://msdn.microsoft.com/cs-cz/evalcenter/dn434042.aspx. (You can eventually use also older versions e.g. 2008) SQL Server has a lot more SQL features and data types than SQL CE, so watch that you use only stuff that is compatible with SQL CE.
In your app.config have you can have two connection strings:
<add name="CompactDBConnection" connectionString="data source=|DataDirectory|\CE.sdf; password=xxxxxx; SSCE:Max Buffer Size=16384; temp file max size=256; ssce:autoshrink threshold=100; ssce:max database size=4091" providerName="System.Data.SqlServerCe.4.0" />
<add name="ExpressDBConnection" connectionString="Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername; Password=myPassword;" providerName="System.Data.SqlClient" />
You can chose then which one to use at the app startup
For creating DbConnection check this C# Retrieving correct DbConnection object by connection string.
Here is a small example of calling stored procedure using DbCommand instead of SqlCeCommand:
DbConnection dbConn = GetConnection(connStr);
DbProviderFactory sqlF = DbProviderFactories.GetFactory(dbConn);
using (DbCommand b2bcmd = sqlF.CreateCommand())
{
DbParameter msg = sqlF.CreateParameter();
msg.ParameterName = "#errorMessage";
msg.Direction = ParameterDirection.Output;
msg.DbType = DbType.String;
msg.Value = string.Empty;
msg.Size = 2048;
b2bcmd.Connection = dbConn;
b2bcmd.CommandType = CommandType.StoredProcedure;
b2bcmd.CommandText = "PB2BImport";
b2bcmd.Parameters.Add(msg);
b2bcmd.ExecuteNonQuery();
result = Convert.IsDBNull(msg.Value) ? "N/A" : (string)msg.Value;
}
I presume you do not use Entity framework - but it would be much easier with it.

ServiceStack OrmLite with multiple Database Servers

I'm building an app around the servicestack framework and need to be able to access data in both Oracle and MS Sql Server. Is this possible using ORMLite, it seems that I can only set a single dialect for the App or have I missed something?
Yes it is possible and support for this is already built into the OrmLiteConnectionFactory, see the Master SQLServer + Sqlite shard example on OrmLite's project home page.
Basically you would register your default (or master) connection first with:
var dbFactory = new OrmLiteConnectionFactory(
"Data Source=host;Initial Catalog=RobotsMaster;Integrated Security=SSPI",
SqlServerDialect.Provider);
Then you would register a named connection for every other connection you wish to support, e.g:
dbFactory.RegisterConnection("shard-1",
"~/App_Data/{0}.sqlite".Fmt(shardId).MapAbsolutePath(),
SqliteDialect.Provider);
Once that's configured, opening a connection without specifying a name will open a connection to the default database, e.g:
using (IDbConnection db = dbFactory.OpenDbConnection()) { ... } //Default DB
Whilst you can specify a name to open up a named connection to a db with a different provider, e.g:
using (var dbShard = dbFactory.OpenDbConnection("shard-1")) { ... } //Named DB
Manually use different Dialect Providers
The differences between the SQL Provider implementations between different RDBMS's are contained within each dialect provider. So if you want to use OrmLite's convenience extension methods against an specific ADO.NET provider implementation you just need to assign the ThreadStatic DialectProvider you wish to use, e.g:
OrmLiteConfig.DialectProvider = SqlServerDialect.Provider;
var dbConn = new SqlConnection(SqlServerConnString);
dbConn.Select<Table>(); //All db access now uses the above dialect provider
This is essentially all what RegisterConnection in OrmLiteConnectionFactory automatically does behind the scenes for you.
For reference here are all the dialect providers for OrmLite up to this point:
SqlServerDialect.Provider
SqliteDialect.Provider (different 32/64 and Mono impls available)
MySqlDialect.Provider
PostgreSqlDialect.Provider
OracleDialect.Provider
FirebirdDialect.Provider

Querying for SQL Servers in an Inno-setup project

I'm creating a setup using Inno-setup.
During the setup process, a SQL Server database has to be created. I want to give the user the ability to select an existing SQL Server instance (if one exists), where the database has to be created.
So, what I want to do in the setup, is to query the network (and the local machine) for SQL Server instances.Furthermore, when the user has selected an instance, I want to verify if there exists a database on that instance which has a specific name.
Anybody who knows how I can do this ? Or maybe someone could give me some pointers in the good direction?
Inno Setup supports the call of external DLL functions, so you should write a suitable helper DLL. Managed .net DLLs can only be used via a COM interface, otherwise you need an unmanaged DLL.
Valid calling conventions are: 'stdcall' (the default), 'cdecl', 'pascal' and 'register'.
Try the following native .Net library call:
using System.Data.Sql;
var instance = SqlDataSourceEnumerator.Instance;
DataTable dataTable = instance.GetDataSources();
The resultant datatable contains the following columns:
ServerName
Name of the server.
InstanceName
Name of the server instance. Blank if the server is running as the default instance.
IsClustered
Indicates whether the server is part of a cluster.
Version
Version of the server (8.00.x for SQL Server 2000, and 9.00.x for SQL Server 2005).

How to read SQL Server Report history programatically?

a SQL Reporting Services Question - for SQL Server 2008.
Given that SQL Server Reporting Services features a Scheduler which can be used to schedule the running of SQL Reports, does anyone know a way to programatically (via C#) read a report's history from the Report Server (and then perhaps retrieve the results of the report)?
So after some more digging, it looks like I need to generate a WSDL for the Report Server and then access information by using the ReportingService object - has anyone done this before (with 2008) and can provide some pointers?
Note: looks like (according to SQL 2008 books online) the WSDL address for SQL 2008 is:
http://server/reportserver/ReportService2005.asmx?wsdl
If I can get this working, I'll post an answer up with the basic steps to implementing it :) It's a little confusing as the documentation is a mixture of SQL 2000 and SQL 2005 references!
OK, so I've actually figured out how to accomplish this seemigly impossible task.
Before I begin, let me just say that if you are working with SQL Server Reporting Services 2008 (SSRS 08) and have (i.e. you have no choice) to use something like Basic auth, you'll only find a world of hurt with the WCF based Service Stubs & IIS. I'm going to blog about the configuration later.
The short answer is as follows:
Connect (e.g. new ReportingService2005() or ReportingService2005SoapClient())
Note: It's easier to use the old (pre-WCF) ASMX service, but not impossible to use the new CF version. The Authentication takes some configuring. There are also some slight syntactic changes between versions.
Find the report history you are looking for, e.g. ReportHistorySnapshot[] history = reportServer.ListReportHistory(#"/Reports/MyHandyReport");
Get the HistoryID from whichever snapshot you want (returned from the ListHistoryReport)
Now, use a ReportViewer to render the historic report, much like you would any other report, e.g.:
ReportViewer rv = new ReportViewer();
rv.ProcessingMode = ProcessingMode.Remote;
rv.ServerReport.ReportServerUrl = new Uri(#"http://localhost/reportserver");
rv.ServerReport.ReportPath = #"/Reports/MyHandyReport";
rv.ServerReport.HistoryId = historyId;
//...snip
byte[] bytes = rv.ServerReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids, out warnings);
Note: you can also use the second WCF Web Service (ReportExecution2005.asmx?wsdl) as well for Report Execution
Well it has soap and extensibility API, perhaps they can be used?

Resources