views:

1836

answers:

5

I can't find how to do this on google anywhere. How do you save to a SQL DB using just C# code? Is it possible? The save method that comes default when you create a DB using the wizard dosen't actually save the DB:

this.Validate();
this.studentsBindingSource.EndEdit();
this.studentsTableAdapter.Update(this.studentsDataSet.FirstClass);
+2  A: 

It looks to me as though you are doing it correctly.

You should check your table adapter and verify that there is an update statement assigned. If you're using sprocs and only have the select sproc assigned then it'll be read only (and won't prompt you for the update/insert/delete sprocs).

Nathan Koop
I'm a newbie when it comes to SQL programming, what are sprocs?
Lucas McCoy
SQL "stored procedures", but since you ask I presume that this is not reason.
Nathan Koop
You would be totally correct in that presumption ;-)
Lucas McCoy
If you do select your tableadapter and then expand the UpdateCommand property, there is a CommandText property, does that have a value?
Nathan Koop
Give me a minute I gotta go find that project....
Lucas McCoy
I can't seem to find the UpdateCommand property in my tableadapter?
Lucas McCoy
you've got your dataset open in design view?
Nathan Koop
Here is what was in that feild: UPDATE [dbo].[People] SET [ID] = @ID, [Name] = @Name WHERE (([ID] = @Original_ID));SELECT ID, Name FROM People WHERE (ID = @ID)
Lucas McCoy
hmmm.... that looks correct
Nathan Koop
do you have Sql Server management studio open?
Nathan Koop
This is weird isn't it? Could there be something wrong with the way my SQL server is configured.
Lucas McCoy
and do you know how to use it? (don't mean to be insulting)
Nathan Koop
I don't have SQL Server Management studio, I only have the express edition.
Lucas McCoy
I don't think it would be configured wrong, updates are pretty standard.
Nathan Koop
can you run "raw" sql commands from it? if so try "SELECT ID, Name FROM People" (if there aren't too many rows in it), I'm wondering if it's actually updating and your application is perhaps caching the data.
Nathan Koop
I guess your right, but hey were talking about microsoft here ;-)
Lucas McCoy
I'm gonna sound like a total newb but my problem is this: I'll post it as an answer so you can see the pic.
Lucas McCoy
+1  A: 

I can't get it to execute my query because I don't know how to do the connection string thing:

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("server=localhost;db=Database1;");
        SqlCommand x = new SqlCommand("SELECT ID, Name FROM People", conn);
        MessageBox.Show(x.ExecuteReader().ToString());
    }
Lucas McCoy
so it would select data even without the connectionstring? weird
Nathan Koop
FYI: a great connection string reference website is http://www.connectionstrings.com/
Nathan Koop
That is the perfect word for it.
Lucas McCoy
Thanks for the link I can't believe I never found this website!
Lucas McCoy
Note: the code you have posted also doesn't open the sqlconnection so you should insert the following as line3: conn.Open();
Nathan Koop
sry we couldn't fix it :-/
Nathan Koop
It's Ok man, It's been bothering me for about 6 months now. I really want to learn SQL programming but it discouraging.
Lucas McCoy
you can delete your email address now if you want.
Nathan Koop
It's actually a different mindset from programming in C#, because SQL is set-based and C#/Java/C++ etc... is more single value based. You iterate over an array or List, in SQL you update the set.
Nathan Koop
I also notice that you haven't opened the connection -- conn.Open() You need to do that.
Cyril Gupta
@Nathan Koop: SQL seems harder (different?) to learn, I don't know this could just be me.
Lucas McCoy
+1  A: 

Your connection string can be a few things depending on how you are configured to log into SQL.

Data Source=ServerName;Initial Catalog=DatabaseName;User Id=myUsername;Password=myPassword;

or

Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI;

The latter is if you are using Windows Authentication; i.e. using the same user account you log into windows with.

DataSource is usually your machine's name or you can use (local) to get you over the hump should it be on the same machine you are working on.

Pat
A: 

i want to develop a c# program to display two shapes(circle and rectangle). the two shapes is enclosed within another rectangle. both shapes moves about inside the outer rectangle. then when you click on either of the shapes, it change to the second one and the position or co-ordinate(x,y) on which the shape is clicked is stored in database. Finally pressing escape exit the application but the total sum of x and y is displayed before exiting.

Adegboye Oluwajoba