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.
Related
I have a project I developed with C# windows forms using SQL Server as database but I want to make the project standalone without any database server. I found SQLite as an option to go for, Is it possible to migrate my SQL Server database to SQLite and not affecting my code? And how do I go about it?
I used entityframework code first in connecting the SQL Server database
The answer is almost certainly going to be "yes." Depending on a few things, you might have to change very little (or no) code, or you might have to change a lot.
The first consideration is your SQL code. If you were very careful to write ANSI-compliant SQL and you didn't use any of the built-in SQL Server views or T-SQL-specific functions, you may not have to re-write much code at all. In reality, you probably will have to at least write some. In particular, while SQL Server's engine is meant to handle multiple concurrent sessions and queries, SQLite is not: you will need to manage your program carefully to ensure no two threads attempt to access the SQLite database at once.
The second consideration is how your application calls the database. Again, depending on your design, you may need to re-write almost no code, or you may need to re-write a lot. In my C# applications, I create an interface for database providers that defines common functionality (select, delete, insert, etc). Then I create simple wrapper classes for different RDBMS that implement the interface. When I need to switch databases, I simply instantiate and use a different class. If you have your project setup like this, then you'd simply need to create a new class for SQLite that implements your database interface and instantiate that instead of your SQL Server class. If you wrote a lot of SQL Server specific C# code into your business logic, you might have a lot of coding to do.
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
How do you design and manage the development of a web application that should be compatible with multiple database management system such as Oracle and MS SQL Server?
If you can't use ORM like NHibernate or EF, how do you maintain database schemas during the development?
My approach now is to have a development database on SQL Server and to port it to Oracle (with a tool) just before releasing a test patch, to test the software on both rdbms. (The tool also generates a file used by the application to upgrade the database)
Is it a good approach? What about a database project of Visual Studio, could it be a better way to keep my db schema?
EDIT:
This question is not about designing the architecture of the application (I have already an abstract data access layer), but how to maintain database schemas for different kinds of rdbms during the development.
I think the key to this is to make sure that you stick to standard SQL syntax. MS SQL Server ships with Transact SQL (T-SQL) which is a super-set of ISO standard SQL, meaning that it has extra syntax which is not officially part of standard SQL.
SQLZoo is a good site that allows you to compare the syntax support of different databases. You will find that most of the syntax that you use from day to day will be the same between most DBs, but there are a few quirks out there. The best way to find them is to check each of your queries in each environment and then check them into source control.
Your idea to use a database project is a good one. It will allow you to deploy your changes to multiple databases quickly and test them automatically.
Model-driven architecture (MDA): use a generic Database modelling tool to design your database schema. You define the tables/relationships/primary keys/etc. in a generic fashion and then have the designer generate the necessary SQL script (most support output to a variety of databases). As you change the DB model, the tool will generate the necessary SQL code to update the database, or generate it from scratch. The tools also assist in generating documentation and assisting with database versioning, amongst many other things...
I use Context Database Designer and am exceptionally happy with the tool and price. Enterprise Architect also looks like an excellent tool, with the ability generate and reverse-engineer code.
Realistically the only way to handle this is to decouple your database access from you core application so that you can customize the access code for each database. In other words, exactly what Nhibernate and EF do. If you cannot use one of these tools you will still effective end up writing one any way. Which can be fun and interesting, but will also take up a large amount of time. So I'd be asking some serious questions about why you cannot use an ORM.
I'm making an application in C# (with Express Edition) for which I would like to add some SQL functionality so that the user can query a database. I don't care how or where I store the database. I can store it in a DataTable or a bi-dimensional array or any kind of file. But I want the user to be able to SQL-query it. Apparently this should be quite simple since the .net seems to be full of database libraries and stuff. I was thinking about downloading MySQL and see if I can connect it to my application. I guess if I want to distribute my application then the user would need to download MySQL as well, which is not a big deal but would be great if I can avoid it. Anyway, for now I would like to start working on my program ASAP, so whatever is the easiest way to do what I want, even if it's not distributable, (but if it is then that's even better), will be good. Thanks in advance.
There are embeddable databases. SQL Server Compact Edition and SQLite are common ones. You can execute queries against these just as you can MySQL or SQL Server.
SQLite (.NET)
SQL Server Compact
You can use most popular databases with .NET. SQL Server, Oracle, MySQL, etc. But you're gonna need drivers of each. So, I'd suggest using SQL Server Express Edition to you to get started.
Then you can easily use SqlConnection and SqlCommand classes to connect and execute queries.
You could use a dbml file in your project and link it to your sql database and then run a sql statement using Linq2SQL documented here
I would look at using and embedded database that you can distribute with your application. SQLite is a very good option. You can then use a free ADO.Net library like System.Data.SQLite for data access. It also provides design time support for Visual Studio.
You can use LINQ to Objects or LINQ to Datasets to run LINQ queries with no database whatsoever. You can't use a bi-dimensional array, but you can use a List<> of objects with properties as a LINQ context.
From your question it sounds like your application, like most applications, may need to store the data for later use: that's where a database will come in handy. .NET Datasets have built in support for persistence to an XML file if your data storage requirements are simple enough to use that. .NET also supports persistence for objects, but you may find that using a database is the simplest solution, especially if you require multi-user access and editing.
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.