0

I'm coming over from PHP and am having a hard time with storing information into my newly created local database. I'm using Microsoft Visual C# 2010 to help me learn and develop.

I'm reading that many people do not like datasets and would opt to ignore them all together. That is fine if I am able to hard-wire into my local database. (I did not use the server database option provided because I'll turn my completed product into a commercial solution and this will require the users to store their information into a local database that stores their project data.

I've made a video showing my windows form and my database, and the extent of my knowledge so far. Maybe you guys can help? http://screencast.com/t/x9Qt1NtOgo6X

1

4 Answers 4

1

There are many ways to access a database from your application. These range from low-level ado.net commands (SqlDataReader, etc..) to using an Object Relational Mapper (ORM) such as Entity Framework.

All of them will require that you learn the technologies, but you can start here:

http://windowsclient.net/learn/videos.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

Heya, are all these usable within the Visual c# framework? I don't feel quite ready to jump onto a new platform. Thanks for the comment! In PHP it was simply , define the database at the header of the script and then use mysql_query() to operate the statements. Is it just as easy today within c# if using the right technologies?
@HudsonAtwell - I don't really understand your question. Any .net technology is usable from any other .net technology. That's one of the benefits of .net is that it is all interoperable with each other.
Thanks, I believe that helps me understand. So I should be able to import an different class to help me make database connections if I need to, such as SqlDataReader. Or use an ORM which seems to be an advanced way of going about sql form relationships possibly.
1

Here's some code that uses SQLServer to do a direct insert, although you'll need a connection string to your database.

Include the SQL server database includes.

using System.Data.SqlClient; 
using System.Data.SqlTypes;

. . .

using (SqlConnection cn = new SqlConnection("XXXXX")) // must put a connection string to your database here
{
    cn.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO Session(field1, field2) VALUES(@Value1, @Value2)"))
    {
        cmd.Parameters.AddWithValue("@Value1", 4);
        cmd.Parameters.AddWithValue("@Value2", "test");
        cmd.ExecuteNonQuery();
    }
}

4 Comments

Posted basically the same solution as mine a few seconds earlier. You left out cn.Open() by the way. :)
Heya, with Windows Visual C#, how would I find my database connection string? I have a connection string for my dataset, which is _this_database conn = new _this_database(); (I believe) , but for my _this_database.sdf file, I've seen no reference to what i should use?
Also, if you would. I like the cmd.Parameters.AddWithValue() , but could we circumvent this and place the values directly into the sql query? One lastee: Why is it called ExecuteNonQuery instead of ExecuteQuery?
To find your connection string - > Data - show data sources - > highlight database and click data source configuration wizard, and it it will reveal the connection string.
1

Well, if you want a quick, almost close to the wire code like the way you used to have with PHP, the following code should work.

var conn = new SqlConnection("Your Connection String");
var command = conn.CreateCommand();
command.CommandText = "insert into sessions (id, name) values (@id, @name)";
command.Parameters.AddWithValue("@id", "");
command.Parameters.AddWithValue("@name", "test");
conn.Open();
command.ExecuteNonQuery();
command.Dispose();
conn.Close();

In the long run, it would be better if you get accustomed to one of the data-related / ORM frameworks such as Entity Framework, NHibernate and the likes. That would really help a lot in data manipulation and make your life a whole lot easier.

5 Comments

Yikes! Shouldn't you have some using statements on those IDisposables?
You will also need to include the header, as stated in the answer by @CommonSense.
@CommonSense, yes, you are right. I was simply lazy to use using and having to indent in the textbox so I explicitly called conn.Close() and forgotten that SqlCommand is also IDisposable. Normally, I would also use using almost everywhere. :)
See I don't even understand the purpouse of the using statement yet. Thanks guys, still going over the answers.
@HudsonAtwell, in .NET, there are classes that implements the IDisposable interface. If the interface is there, then we should call the Dispose method, so that the object gets cleaned up properly. In this case, SqlConnection and SqlCommand implements IDisposable, so once we are done with it, we should call Dispose for both. I'm not sure what SqlCommand.Dispose would do, but SqlConnection.Dispose is basically the same as calling SqlConnection.Close. By having using (...) { ... }, the Dispose method will automatically be called at the end of the block.
1

It depends on your requirments, but for most situations, I would highly recommend you use Entity Framework or Linq to Sql data classes. You'd be much better off... go with the latter as a start... hope it helps.

[Edited]

If you want to see how easy an ORM can be:

  1. right-click on your project
  2. select Add New Item
  3. Choose Linq to Sql Data Classes
  4. When you've added it, you'll have a blank .dbml file
  5. Go to server explorer and add a connection to the sql db
  6. Drag and drop the tables wherever you like
  7. Start using the entities like this:

    using (DataClasses1DataContext db = new DataClasses1DataContext("Data Source=localhost\sqlexpress; Initial Catalog=myDBName; Integrated Security=true")) { IEnumerable citiesForUSA = db.Cities.Where(x => x.Country.Name == "United States");

    City city = new City();
    city.Name = "Metropolis";
    //etc
    db.Cities.InsertOnSubmit(city);
    db.SubmitChanges(); // <-- INSERT INTO completed
    
    //etc
    

    }

Good luck!

:-)

3 Comments

Hey @Matt, So I'm wondering, what's the reasoning behind using datasets rather than live sql interaction?
@HudsonAtwell - there's no such thing as "live sql interaction". What exactly are you referring to? DataSets are just containers to return the results of query.
I see. I believe you've helped explain it right there. A dataset will help keep the data on-hand, and any changes to the data-set will be reflected anywhere an object is tied into that data-set. Is that correct? When I said live sql interaction I meant that if an row is inserted on the backend into the database, any objects displaying that sql data would auto-display the newly inputted data along with the old data. This would also be known as asynchronous updating. But logically I think I would have to force a data-refresh to show the newly added data, unless data-sets allow for this...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.