Using MySql stored procedures for .NET Data Access Layer - c#

I'm using .NET 2.0 and/or 3.5. Weird thing is, everytime I add a query, whether via TableAdapter or a plain query in a Dataset, using a MySQL stored procedure (whether be a select, update, insert, delete), the wizard doesn't finish (the dialog suddenly disappears, I'm back to designer mode and the query isn't added to the tableadapter or dataset form). Is there a special formatting required for MySQL stored procedures, or a workaround for MySQL stored procedures to work?
I'm using
MySQL 5.1.33
portable XAMPP 1.7.1
PHP 5.2.9
Apache 2.2.11
phpMyAdmin 3.1.3.1

I was having this same thing happen. The fix for me was the following: One of my parameters for the stored procedure was 'filter'. I changed it to 'p_filter' and the issue went away. The issue that I am still dealing with is on the last screen of the filter I get:
you have an error in your sql syntax; check the manual that corresponds to your mysql server version for the right syntax to use near 'NULL' at line 1
I brought in a stored procedure that doesn't accept a parameter and I have no issues with creating the TableAdapter using the dataset wizard.

Related

C# ANSI Database Bulk Insert

I am looking for a way to do multi row inserts with C# which can be universal enough so that the same procedures can be applied to more than just SQL Server.
Right now, we have an application that can use SQLite, PostgreSQL, and SQL as a back end to store data, with a possibility of adding Oracle into the mix as well. For that reason, it would be great to have a single procedure that can insert multiple rows in one round trip, and for that same procedure to work regardless of which Data Client is being used. (For this reason, I am not using a SQL Server Stored Procedure).

How to input data into SQL Server for first time start of C# application

I created a C# program using SQL Server 2008 Express.
There is some data that must exist in the database table initially, for the C# application to run correctly.
I would like to transfer this data into the table the first time the C# application executes.
But I don't want this SQL data (record data) read from my C# code (I don't want to load this data from hard-coded C# code).
How does SQL Server store its record data, and how can I transfer the initial data into the database?
I'd try to not rely on a database being existent for your application to work. You're coupling yourself to the database which probably isn't a good idea. Do you think maybe you could execute a stored procedure from your application which would fill out the tables you're relying on? That way your DB creation code wouldn't exist in your application. This would however still mean your application is dependant on the database to function correctly.
I would recommend one of two plans, the latter being the cleaner.
Plan 1
Create a SQL script which will create and insert all the required data for the application to work and place this in a stored procedure
When your application starts first time it will check a configuration file to see if the program has ran before, if not, execute the stored procedure to create the required data
Alter an external configuration file (which could be password protected) which will indicate whether the stored procedure has already been run, could just be a simple bool
Each subsequent time the application runs it will check to see whether it's run before and won't execute the stored proc if it has
Plan 2
Create a SQL script which will create and insert all the required data for the application to work and place this in a stored procedure
Use an installer to deploy your application with a custom action to create the database, explained here
You definitely need to have the SQL insert/create script at least partially hardcoded somewhere. Either in an SQL script sitting in your app's directory (not recommended because the user might tamper with it) or in your application's ressources (a bit more secure as the user would need to hex-edit the EXE or DLL).
Tried this ? http://social.msdn.microsoft.com/Forums/en/adodotnetdataproviders/thread/43e8bc3a-1132-453b-b950-09427e970f31
I think your question is so simple and you want to fill the tables before you run the program.
simplely you can open sql server managment studio and and connect with your instance name,then expand Database list and find your database that you have created.expand that and in Tables tree find the table you want to add data to it.right click on it and do Edit
also Redgate has a product to fill tables called data generator,that can use to fill your tables with so many data

How do I populate a set of Entity Framework Code first POCOS from SQL server (once off)?

We have custom code that wipes and initializes our test database by creating a few objects and saving them to the DatabaseContext (we use Migrations for the schema, but not for the data). However, our tester has created a lot more data, and doesn't want to lose her test data when we do a clean deploy. I therefore have to figure out how to get the data from SQL Server into C# (once off), so it an be run with our data-initialization program.
My initial approach was to manually copy-paste the data into C# object initializers, but this got old quickly, since there are 100+ rows to copy.
My second approach was to create the objects using a SELECT statement in SQL Server, mixing the C# initializer syntax into the SQL SELECT statement, and then copying the C# strings generated by SQL Server back into Visual Studio.
Although the second approach seems OK, I was wondering if there's an obvious solution (apart from simply running a SQL INSERT script every time) that I am missing.
I ended up going with the approach mentioned in the question. Generating C# object initialializers in SQL Select Statements.
A few things I picked up, mostly obvious:
CAST AS VARCHAR for all numeric fields (although SQL Server will give error if you don't).
Dates: convert to VARCHAR and surround with DateTime.Parse()
Use a CASE statement to convert SQL bool value (1/0) to true/false as strings.
Enums: generate a cast.
If using autonumber ids, remember to prefix instance names with a letter(s). Add the object to DataContext after initializing it.
Don't be afraid to reference your C# code in the generated code. It isn't going to run in SQL Server in any case.
I would just create an SQL script that recreates the data in your DB.
You can run this script through the commandline or through C#...
You do have another option. Take a backup of the test database, load that backup into a new database on the same server as the target database, and when the migration is finished (including clearing the database) issue cross-database INSERT...SELECT statements.
The benefit of this approach is if the testing team gets another version they like, you can take a backup and overwrite the staging database with that allowing them to continually build more data.

Entity Framework & SQL Server 2008 Express

I'm totally new to Entity Framework and have done some reading, and as a test I have put together a very brief test framework with just two small Entities. I then right clicked and selected "Generate Database from Model" which takes me to the SQL Connections page. However, none of the previous connections I have used appear in the drop down list, and when I select Create New Connection I only have the options to use 'SQL Server Compact 3.5' or 'SQL Server Database File'.
I have come across this before with SQL Express and the work around is to create my own Connection String to access the required Database. However, with me using Entity Framework to create the database, it is impossible to write an appropraite connetion string.
I therefore seem to be in a Catch 22 situation.
I cannot write a connection string until the database is created.
I cannot create the database from EF without accessing SQL Server (via a connection string).
Anyone come across this or can point out what I'm doing wrong. Like I said I'm totally new to EF so I apologise if this is a very basic question.
Unless you want to use a database file in a user instance, you need to either use SQL Management Studio Express, or use the SQLCMD command line tool to create the database. I would recommend SQL Management Studio Express as it is easy to learn in my opinion.
The Entity Framework tools are intended to be used to create the database schema, not the database itself. You still need to define the file groups, security information, and other basics of creating and configuring a blank database.
See this Q/A for appropriate links:
How to create DB in SQL Express using SQL commands?
Thanks #psuedocoder, your assistance helped me resolve the problem. The answer in the end was simple, but not intuitive to me, hence my difficulty. I thought some elaboration on the answer might help others who are equally new to Entity Framework as me.
From Visual Studio
Create your EF model in Visual Studio.
Right click the model canvas and select 'Generate Database from Model'.
You are then asked to select the database connection. Select 'New Connection'.
In the 'Add Connection' dialog box, rather than 'Browse' for an existing database, just simply type the name of your new database in the Database text box and Click Connect.
When you try to connect you will get a warning saying the database does not exist, but you will be asked if you want VS to 'Create It'. Select Yes.
As #psuedocoder states this does not actually create the database in SQL Server, but it does create an object in your VS soluton which contains the TSQL script required in order to create your database.
From SSMS
Go to SQL Server Management Studio and create a blank database of the same name used in step 4 above.
In SSMS select open file and navigate to the windows folder containing your VS solution files.
Open the TSQL script file. This will have a DB Script icon and have an .edmx extension.
Click Execute, and you will have your new database created from your EF model.

Passing a dataset to a SQL Server stored procedure

Here I am facing a problem that I want to pass a dataset to a SQL Server stored procedure and I don't have any idea about it and there is no alternate solution (I think so ) to do that, let me tell what I want ...
I have an Excel file to be read , I read it successfully and all data form this excel work book import to a dataset. Now this data needs to be inserted into two different tables and there is too many rows in Excel workbook so it is not good if I run it from code behind that's why I want to pass this dataset to stored procedure and than ........
please suggest me some solution .
Not knowing what database version you're working with, here are a few hints:
if you need to read the Excel file regularly, and split it up into two or more tables, maybe you need to use something like SQL Server Integration Services for this. With SSIS, you should be able to achieve this quite easily
you could load the Excel file into a temporary staging table, and then read the data from that staging table inside your stored procedure. This works, but it gets a bit messy when there's a chance that multiple concurrent calls need to be handled
if you're using SQL Server 2008 and up, you should look at table-valued parameters - you basically load the Excel file into a .NET DataSet and pass that to the stored proc as a special parameter. Works great, but wasn't available in SQL Server before the 2008 release
since you're using SQL Server 2005 and table-valued parameters aren't available, you might want to look at Erland Sommarskog's excellent article Arrays and Lists in SQL SErver 2005 - depending on how big your data set is, one of his approaches might work for you (e.g. passing as XML which you parse/shred inside the stored proc)

Categories

Resources