Sign in to follow this  

creating a simple database from scratch with sql and c#

This topic is 3462 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all, i'm wondering if anyone can point me to an online tutorial, i have sql express 2005 and visual c# 2008 and am wanting to try and make a database from scratch. the books i have on the subject all show me how to connect and modify stuff in the example "northwind" database, but not how to setup an empty database ready for use. any links would be appriciated. thank you.

Share this post


Link to post
Share on other sites
Are you wanting to create a brand new database from scratch and then access it using a C# program, or write a C# program capable of creating a new blank database on a server for you?

This isn't completely clear, I'm afraid.

Share this post


Link to post
Share on other sites
i want to write a c# program, design the user interface, then bind the textboxes to the new database columns that are created within visual studio as a new sql database, so that when i run the program it's empty, but i can enter, save data etc on the database.

You'll have to forgive me if i'm not making any sense as i'm not entirley sure how it works, i have all the code that tells me how to do things like add records, delete, search and so on but not how to start a blank c# database program

Share this post


Link to post
Share on other sites
Connector/Net can do all of that for you. Few simple commands to get connected the the database and then you construct SQL queries and it passes them along to the database. I had a program creating/reading/writing to tables in like an hour or so with no previous experience with MySQL.

Share this post


Link to post
Share on other sites
Quote:
Original post by JonathanCCC
i want to write a c# program, design the user interface, then bind the textboxes to the new database columns that are created within visual studio as a new sql database, so that when i run the program it's empty, but i can enter, save data etc on the database.

You'll have to forgive me if i'm not making any sense as i'm not entirley sure how it works, i have all the code that tells me how to do things like add records, delete, search and so on but not how to start a blank c# database program


Ah, I see. I did something similar for a lab assignment recently except I was using VB. I've hardly touched C# except with XNA so I can't really help you there. Our database was stored on a separate server and we had the login details for it, so all we had to do was open a connection inside the program to do what we needed to do with the database.

The actual code required to access a database, view records, update records etc. is actually pretty straightforward. Pretty much everything you need is inside the .Net framework somewhere, it's just a case of knowing what to do and when. There's plenty of data binding options available, except I didn't use them; I created strings programatically which were SQL queries, then you can pass the query string to the SqlDataAdapter which will then execute it for you...

It's easier than you might have thought at first, especially if you're doing it as a personal project and not some business-critical $1m enterprisey thing.

Share this post


Link to post
Share on other sites
"Head First C#" goes step by step how to do this in the first chapter of the book if I remember correctly. It's pretty cool and so simple I could probably recite the steps from memory but don't have time right now.
Also I'm pretty sure the Micrsosoft visual studio website has a video on it. They got video's for everything nowadays you can check out and that'd be the easiest for sure!

Share this post


Link to post
Share on other sites
many thanks for the replies.

I went on an followed this tutorial:

http://support.microsoft.com/kb/307283

however when I click "create database" on my form, I get the following error:

System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at WindowsFormsApplication1.Form1.btnCreateDatabase_Click(Object sender, EventArgs e) in C:\Documents and Settings\Tony\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Form1.cs:line 38


does anyone know how to fix this? i'm using windows XP media edition and visual c# 2008 and sql server express 2005 with service pack 2. I have all admin rights.

Share this post


Link to post
Share on other sites
Quote:
Original post by JonathanCCC
many thanks for the replies.

I went on an followed this tutorial:

http://support.microsoft.com/kb/307283

however when I click "create database" on my form, I get the following error:

System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at WindowsFormsApplication1.Form1.btnCreateDatabase_Click(Object sender, EventArgs e) in C:\Documents and Settings\Tony\Local Settings\Application Data\Temporary Projects\WindowsFormsApplication1\Form1.cs:line 38


does anyone know how to fix this? i'm using windows XP media edition and visual c# 2008 and sql server express 2005 with service pack 2. I have all admin rights.

Run something like SQL Server management studio to make sure your database is up and running and that you have the name correct.

Share this post


Link to post
Share on other sites
ok I finally set one up, and am able to add records and update the database. But when I delete or edit a record and try and update, I get this error:

Update requires a valid UpdateCommand (or when deleting says DeleteCommand) when passed DataRow collection with modified rows.

the code for my delete button is:

DialogResult dr = new DialogResult();
dr = MessageBox.Show("Are you sure you want to delete this record?", "Delete?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
if ((this.BindingContext[objDataSet1,"Table1"].Count > 0))
{
this.BindingContext[objDataSet1,"Table1"].RemoveAt(this.BindingContext[objDataSet1,"Table1"].Position);
this.objDataSet1_PositionChanged();
MessageBox.Show("Record deleted", "Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}



And the code for my update

try
{
// Attempt to update the datasource.
this.UpdateDataSet();
}
catch (System.Exception eUpdate)
{
// Add your error handling code here.
// Display error message, if any.
System.Windows.Forms.MessageBox.Show(eUpdate.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
this.objDataSet1_PositionChanged();


if someone could point me in the right direction or to an online source would be great thank you.

Share this post


Link to post
Share on other sites

This topic is 3462 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this