ps: I'm not sure "replicating" is the correct term for this.
other than the SQL statements/stored procedures defined for the application's use, we have to use cmd to execute anything on the database in case only SQL express is installed (without management studio). no?
since that is out of my control (I mean to have management studio or not) I'd like to provide my application's super admin(IT supervisor in my case) with GUI similar to SQL server management studio new query option.
the basic idea would be to have a textarea to enter SQL command, a submit button and datagridview to display results. this part is feasible.
but I'm debating the idea itself, is it worth being implemented in the first place? what are the trends relating to this kind of situations? any best practices?
if we were to expand the basic idea, I have 2 points:
- SQL server management executes multiple queries at once and displays them is the same result window even if they don't match in columns, would that map to multiple datagridviews?
- SQL server management have a messages tab to display execution related notes, how to get those message in the application in order to be displayed as well?
this example of management studio from MSDN
any pointers to resources, possible duplicates or even the correct SE community I should've posted this on (I tend to mix them up :)) are most appreciated.
Thanks
update:
I don't mean providing this functionality for all the users of the application.
since I'm both the IT supervisor and the developer, anything related to the application will end up being brought to me. so I have access to the database anyway that's not a security breach.
but instead of having the application in developing cycle I'd have on-spot fix capability.
for example:
if an urgent report was required: I'd query the requested data in the private IT section without delay, then make changes to the application with no time-pressure.
Have you already considered using SQL Server Management Objects?
It is basically the same library in which SQL Server Management Studio (and the Express Edition) are based. So you wouldn't have to worry on differences about how you run/execute your queries.
Also, you can redistribute the necessary .dlls, without the need of installing the SQL Server Management Studio itself: http://technet.microsoft.com/en-us/library/ff713979%28v=sql.105%29.aspx
In our company we use a tool that can run SQL Queries like the one you want, getting information from those objects (SMO) with the same SQL Messages outputs and tables (you can use it to create your gridviews) as SQL Management Studio does. But we use it for the purpose to automate customer deploys/patches.
Katia,
You want to run ad-hoc read-only queries against your database and then give the results to your client.
Make sure you have appropriate database permissions
Use the SqlCommand object to send in the sql
Get the stuff back as a DataReader (reader)
Iterate over the column names with reader.GetName(i)) for reader.FieldCount
While reader.Read iterate over the column values with reader.GetValue(i) for reader.FieldCount
Parse the above into an HTML table
Send the HTML table to your client and get them to open it in Excel
Make sure and include the query that you used so that you can replicate/refine
And we're done.
PS Let me know if you need more implementation details
Related
It is well known that perfomance wise, it is recommended to use SQL Server stored procedures instead of inline code. However, I still use inline SQL queries in Visual Studio for various reasons:
The queries can be neatly organized in separate text files (.sql) and in a folder structure.
The files are part of the Visual Studio solution and thus submitted to source control.
Changes to SQL queries can be published together with the applications (using WebDeploy for ASP.NET apps or ClickOnce for Windows apps).
There is no need to synchronize changes to the SQL queries and publishing new versions of applications.
It enables me to work on the SQL queries even when I am offline (or without access to that particular SQL Server).
I am not quite ready to give up these advantages but still, I am aware that I am sacrificing performance.
Is there a way to get the best of both worlds?
Thanks in advance for any insights.
Chris
Literally every single one of your points can be provided by Stored Procedures too... Not only could you just have a .sql file with the CREATE or ALTER command for the stored procedure in the exact same way you manage it now, but you could go a step further and use a SQL Database Project type to deploy them in a better manner...
https://msdn.microsoft.com/en-us/library/xee70aty(v=vs.140).aspx
But I will note that stored procedures are not automatically better for performance... If you read this is probably refered to the fact that they are easier to parameterize, so the plans can be resued. Using proper Parameterized queries you will have the same benefits, so I think the basic premise of your question is incorrect.
I still use inline SQL queries in Visual Studio ...
But how? Context is important here. VS is just a tool. If you use inline queries in your app, then you have a potential security risk if you are not careful about how you implement them (re: sql injection). In addition, the use of inline queries requires the appropriate permissions to database objects - another security risk. And this approach creates a dependency between your code and the schema - which is minimized by using procedures.
As a beginner in C#/VS I want to practice making basic LINQ queries on simple DB's. I downloaded the "famous" Northwind and Pubs databases on the official Microsoft websites (https://www.microsoft.com/en-us/download/details.aspx?id=23654). It gave me a .msi files that installed 6 files on my c: instnwnd.sql, instpubs.sql, NORTHWND.LDF, NORTHWND.MDF, PUBS.MDF, PUBS_LOG.LDF.
When I launch Visual Community 2015 and simply make an "Open => File..." and open the "instnwnd.sql", it opens the .sql file without problem. The problem is when I'm trying to make a connection to the database, being either through the Solution Explorer (Data Connections => Add Connection) or while I'm trying to create an EDM (Database first), which is what I want to do eventually.
The exact problem is that one : the Northwind and Pubs databases doesn't show up in the database list when i'm trying to add a connection, I only get the system databases. Here is what I get :
http://img15.hostingpics.net/pics/128870screenshot1.jpg
I read many MSDN documentation pages and several StackOverFlow topic about the matter (the closest of solving my issue was that one, but didn't work :How to connect to LocalDB in Visual Studio Server Explorer?). I have SQL Server Express 2014 with tools, started SQL services in my Windows Services etc.
What should I do in order to have those databases available so I can generate code and starting making my LINQ exercices ?
Thanks in advance !
You need to attach the databases before you can see them**. You're having a problem attaching the databases due to versioning (which is odd), but your sample db's are very old. Perhaps try newer ones from here https://northwinddatabase.codeplex.com/ and make sure you attach them before expecting them to show up in the tools.
** Technically this is not true. I believe there is a way of connecting to sql server and passing a file path in the connection string, however this will likely still fail for your due to the error you get trying to attach the dbs you have. Additionally, it requires a specially crafted connection string which may or may not be usable with the gui tools you're trying to use. It is also, in my opinion, a less common way of accessing sql server databases, so I would stick with attaching them first. If you want to find out more about this, I believe the feature you need to research is called 'sql server user instances'.
I have an c#.net windows based application that uses a database in Microsoft SQL Server 2008. During deployment for very first time to our client(s), we create a copy of our database and deploy it on client(s) remote server along with the UI application. The client database can be on version SQL Server 2005 and higher.
During times the UI application and associated database has gone lots of changes. Since this is a thick client application the client(s) database is not sync with our latest database and unfortunately no one ever made notes of all the changes done. So my challenges are as follows:
How to find any missing columns on database table in Client's Database as compared to my Database? if any?
How to find any missing Primary/Unique Constraints on database table in Client's Database as compared to my Database? if any?
How to find any missing Indexes on database table that exist in Client's Database as compared to my Database? if any?
Please keep in mind the client(s) database size may ranges from 10-100GB, so i cannot plan to just drop all client tables and recreate it.
You can use Data-tier applications. It's built-in feature of SQL Server, so you don't need to use any extra tools.
You can extract data-tier application from your database (in SSMS right-click -> Tasks -> Extract data-tier application) to a DACPAC file, copy the file to the client's server and use it to upgrade the DB there (or generate update script).
It also integrates nicely with SQL Server Data Tools.
For this task, you need a software that compare SQL database. Just like there is a lot of software to compare text, there is a lot to compare database.
Personally, I use AdoptSQLDiff, but there is a bunch. RedGate has developed one also and I know others exists. Just type SQL Database compare in google to find them. You probably can have the job done with the trial period.
These softwares show you which tables was added, deleted or changed. It does the same for views, indexes, triggers, Stored Procedures, User Defined Functions, Constraints. More importantly, those tools generate script to push modifications into the target database. Very handy, but have a look at the script generated, it sometime messes it up by deleting data, but it can be fixed very easily.
There is also the option to compare data in a specific table if you need to.
Here is a screen shot of the interface of another so you know what it's look like.
With SQLServer Management Studio, you can try selecting a database and then Task->Generate Script, selecting appropriate options.
Do the same thing for the 2 db you want to compare. You will get two text files you can compare with a text file software comparer.
Comparison will highlight difference in the db structure.
Not the best way to do it, of course. But it can be a start. If the two dbs are not too different, you should be able to handle the differences
Better option, use some db comparer software. They are meant to compare db structure, constraint indexes and so on. Never used any of them, so cannot give any advice on that
If it is one time thing use any diff tool for DB, VS2010+ has a build in one, allows you to get difference for schema and data in two different files.
If you want to solve problem of your development process, you have wide range of options to implement versioning for data base.
If you are using EF - use Migrations, can't beat that.
If you are only on SQL Server and never looking at other RDBMS, check DAC ( Data-Tier applications, mentioned by Jakub)
Otherwise take a look at more generic solutions, among them I would reccomend you to take a look at DB.UP and if python code is good for you , check Alembic, it allow you to write your migrations using really nice python API.
if nothing works for you, create snapshot of current db schema and start doing differential scripts that you can use with self written tool or DB.UP
I am not sure if this can help, but who knows.
So is there any way to restore the server database on your local environment? If the answer is yes, you can try to join system views for each database and compare them?
I propose something like this(was a quick solution, so please sorry for formatting and other common stuff).
USE [master]
GO
SELECT
LocalDataBaseTable.name AS TableName,
LocalDataBaseTableColumns.name AS [Column],
LocalDataBaseTypes.name AS DataType,
LocalDataBaseTableColumns.max_length,
LocalDataBaseTableColumns.[precision]
INTO #tmpLocalInfo
FROM LocalTable.sys.columns as LocalDataBaseTableColumns
INNER JOIN LocalTable.sys.tables AS LocalDataBaseTable
ON LocalDataBaseTableColumns.object_id = LocalDataBaseTable.object_id
INNER JOIN LocalTable.sys.types AS LocalDataBaseTypes
ON LocalDataBaseTypes.user_type_id = LocalDataBaseTableColumns.user_type_id
SELECT
ServerDataBaseTable.name AS TableName,
ServerDataBaseTableColumns.name AS [Column],
ServerDataBaseTypes.name AS DataType,
ServerDataBaseTableColumns.max_length,
ServerDataBaseTableColumns.[precision]
INTO #tmpServerInfo
FROM ServerTable.sys.columns as ServerDataBaseTableColumns
INNER JOIN ServerTable.sys.tables AS ServerDataBaseTable
ON ServerDataBaseTableColumns.object_id = ServerDataBaseTable.object_id
INNER JOIN ServerTable.sys.types AS ServerDataBaseTypes
ON ServerDataBaseTypes.user_type_id = ServerDataBaseTableColumns.user_type_id
SELECT
#tmpServerInfo.*
FROM #tmpLocalInfo
RIGHT OUTER JOIN #tmpServerInfo
ON #tmpLocalInfo.TableName = #tmpServerInfo.TableName COLLATE DATABASE_DEFAULT
AND #tmpLocalInfo.[Column] = #tmpServerInfo.[Column] COLLATE DATABASE_DEFAULT
WHERE #tmpLocalInfo.[Column] IS NULL
DROP TABLE #tmpLocalInfo
DROP TABLE #tmpServerInfo
This will return all information about missed columns in your local database. The idea is to investigate 'sys' views and to find out if there any suitable solution for you.
You can use this simple script, which show you differences between tables, views, indexes etc.
Compalex is a free lightweight script to compare two database schemas. It
supports MySQL, MS SQL Server and PostgreSQL.
or look at this question Compare two MySQL databases. This question about comparing two MySQL schemas, but some of listed tools supports MSSQL or have MSSQL version (for example http://www.liquibase.org/).
Another answer What is best tool to compare two SQL Server databases (schema and data)?
How do i create a db file in C#? a friend told me it was in the toolbox and not to use sqlite. I dont see anything that could be it, nor what it is called. google didnt help:(
Could it be...
SQL Server Compact Edition – A lightweight, in-process database engine designed to run on devices and desktops and is geared toward local data storage. Compact Edition includes a subset of SQL Server 2005 data types and shares common elements of the Transact-SQL (T-SQL) language with the data service engines.
There is no file-based database provider built in to c# or the .NET Framework. There are of course pre-existing connectors for using SQL Server (which includes SQL Express), but if you need a fully functional RDBMS that is file-based, you need to use something like SQLite or Firebird (also a fan of VistaDB, not free or open source but VERY solid and pretty affordable).
On the Add New Item menu: "Service-based Database" or "Sql Server Database" if it is an asp.net application. I am sure your friend meant it as "create a sql express db file in Visual Studio".
That said, if you wanted to fill an empty database, with tables that correspond to a c# model, you could create a linq2sql model, and use its CreateDatabase to do that for you :)
You might want to check this http://quickstarts.asp.net/QuickStartv20/aspnet/doc/data/vwd.aspx (visual web developer link, but it applies).
For a full/in-depth explanation of how SQL express can be used with a semi file based approach check and its limitations:
http://www.databasejournal.com/features/mssql/article.php/3704171/SQL-Server-2005-Express-Edition---Part-8---XCopy-Deployment.htm
Perhaps you might try Microsoft LocalDB.
It is file based but uses a low level SQLExpress Installation to host it.
If you are using Visual Studio or Web Developer Express, there are indeed ways to easily create a MS SQLExpress database. Just go to Add New Item... and it should be one of the available file types.
Keep in mind you have to have installed either Microsoft SQL Express Edition (free, as in beer!) or Microsoft SQL (very un-free!, in all senses). If you haven't done this, you don't get the option of creating a database file so easily. If you have got it yet, you can get it here.
As other answerers have mentioned, strictly speaking this is NOT a C# feature. MS SQL and it's derivatives, are database applications, much like Oracle, MySQL, or PostgreSQL. It's just that Microsoft Visual Studio makes using the Microsoft database product very easy by default. Differentiating between C#, Visual Studio, and any database programs will probably get you better answers, faster, no matter where you ask. :)
Every database has a file system in some binary format more than likely custom and uses a cache to control the flow of the database(s) lifetime.
If you create a database system, you will need some type of cache because you only want to read from the file if the cache has already released it.
If you have 1000 clients tapping into the same db, you certainly don't want to read/write to the file for each client request, so you want to manage a queue of clients and run it against the cache so that the cache knows not to release the db after its time span for lifetime is reached put to rather update the time span, therefore, not having to reload the file, if disposed and queued again, until the queue referencing the db object is empty.
Creating a well designed cache it used by all rdbms's so that duplicate objects are not created and files are not reloaded if not need be.
You can use
FileDB - A C# database to store files
http://filedb.codeplex.com
There is a MVC Example in the source that allows you to upload files and also has drag and drop support.
It then saves it into just one file in a location that you specified.
Like this:
private string pathDB = #"C:\CMS-MVC\Parts\FileManager\filedb-19055\trunk\MvcTest\Data\MvcData.dat";
That one file will store all of your files in that one "container".
You are mistaken. Databases are not developed in C#. Databases are built using a database system such as Oracle, MS SQL Server, MySQL, and numerous others.
Once you build a database using one of the above providers, you can then perform actions on the database using your programming language of choice (in your case C#) to get data out of and put data into it.
I was wondering what and why you would choose to be able to make a database that can support no more than 100 users with no more than 10 using it at once with a Visual Studio 2008 C# Windows Form front end to access it by. I have to access the database over a network connection, not just on the local machine. I also need to define where the database is found at run-time in the code as opposed to the "Data Source" view in Visual Studio. If my question needs reframing or is not understood, let me know and I will adjust. Part of my problem is I am not sure even how to ask the right question, much less what the answer is.
If it is not for comercial purposes you can try SQL Server 2008 Express. It can integrate nicely with Visual Studio 2008 for development and has support for LINQ, Entity Data Model and ADO.NET Entity Framework to make it easy to create next generation data-enabled applications.
http://www.microsoft.com/express/sql/default.aspx
You can also store your connections strings in the application configuration file and retrieve them programatically for setting up the database connection.
http://www.codeguru.com/columns/DotNet/article.php/c7987/
I would probably go with Sql Server Express, it's free and works well with .NET. Assuming your schema is not changing at runtime you can probably still use the design time data source features in Visual Studio. The connection information is stored in the app.config file which you can update after the app is deployed to point to a different database. You can also develop a class that gets the connection info from somewhere else as well and just use that when you need to open a database connection.
I know using mssql you can pick between different connection strings for all of your db calls, just do something like
Command.Connection = GetMyConnectionWithWhateverLogicINeed();
I'd have a look at Sql Server Workgroup Edition
http://www.microsoft.com/sql/editions/workgroup/
Express edition used to have some limiting features for more than about 5 users and it is not supplied with any management tools which is a bit disheartening.
I'm not sure I totally get what you are asking, Matt, but I can tell you that I developed a series of apps written with VS 2008 and we used a MySQL DB for it. While I'm definitely not a DB guru at this point, I've not had many issues with using MySQL.
Perhaps if you rephrase your question, we can provide better answers.
SQLite for sure.
ADO 2.0 Provider