0

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?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Mahdi Radi
  • 429
  • 2
  • 10
  • 30
  • you mean what, when you enter anything or press any button then data shoul record in the database? – Sohail Nov 26 '12 at 21:02
  • i mean, at first time run of my application, sql tables fill with data (Record) . because this data must be exist for run application. – Mahdi Radi Nov 26 '12 at 21:14
  • so it means when you run the application all the sql data should appear, right? – Sohail Nov 26 '12 at 21:18
  • @Mehdi Radi Are you trying to do a deployment? Are you gaurented the client is going to have sql server, with the database attached? – clamchoda Nov 26 '12 at 21:19
  • when run application at first time. the sql database has create and tables create too. but all of table is empty And my c# application need some data in sql for work. and my problem is that i don't know my sql data read from where (sorry for weak english) – Mahdi Radi Nov 26 '12 at 21:36
  • The inserts will have to exist somewhere. If not in the code then in a file that is executed by the code. – krawl Nov 26 '12 at 21:44

3 Answers3

0

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
Mark Walsh
  • 3,241
  • 1
  • 24
  • 46
0

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

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
0

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

Arash
  • 3,013
  • 10
  • 52
  • 74