I am currently doing a project where i am using C# and databases. I am using visual C# 2008 and i am having issues accessing the tables that i made.

So what i have done is that i have a couple of tables, one with students in it, another with classes in it. I am making a third table that links the two through an ID. So what i need to be able to do in my program is to be able to select a row from a DataGridView (what you get when you pull the table onto a form by default) and then i need to be able to add a value from that row to another database.

So i am currently able to get the value that i need from the selected row. My main problem is once i have the data that i need to put into the table, i don't know how to do it.

I keep seeing code where people have SqlConnection types yet i cant find that in my program. I don't know if this is because i am using visual C# so therefore something is loaded differently, but i would like to know how i would go about adding a row to a table in a database in C#.. or how i would find the data necessary to connect to the database with visual C#.

One last thing, i wouldn't mind more on the whole C# and databases, so i was wondering if anyone knew of any good tutorials, it would be great if they used visual C# rather than just typing. I can do both, its just when trying to develop quickly being able to use a form builder is great :)

Cheers
Paul

What type of database are you using?

What type of database are you using?

Thread name SQL in C# :)

commented: Unconstructive. There are many types of SQL driven databases. +0
commented: N/A +10

Yes, but

I keep seeing code where people have SqlConnection types yet i cant find that in my program.

would indicate the maybe he has only created a DataSet and has not created a database connection yet.
Also, there are several flavours of SQL.

OP: If you're using Microsoft SQL Server, you can use a handy piece of .Net 3.5 functionality called LINQ to SQL. This will handle most, Update/Insert/Select functionality for you. It is in-fact, very, very good.

If you're using MySQL or other type of database. Then you will need to find an ODBC driver for that database. MySQL's can be found at their Development (GA) website called MySQL Connector/NET.

There's plenty of documentation surrounding both LINQ to SQL and MySQL Connector. Unfortunately, I'm limited to those two, so I know little else about Oracle and other flavours of Databases.

commented: MySQL not using SqlConnection -1

Okay, so i am a bit of a beginner at C# so im sorry i didn't provide that info at the start.. Im not exactly sure what kind of database i am using but i'll tell you how i added it, that may help you understand what kind it is.

I went to the database explorer and right clicked on Data Connections and selected Add Connection. The data source that i used was "Microsoft SQL Server Compact 3.5 (.NET Framework Data Provider for Microsoft SQL Server Compact 3.5)" and it was stored on my computer. Then i made a few tables and then added a couple of Data sources.


You know what? I'll attach a screenshot. That could help.

Thanks for your help guys :)

By the way, i have heard a lot about LINQ, can anyone tell me what this is quickly or should i just google around? :P

When you say that you need to add that data to another database, did you actually mean table?

When you use a DataSet you can simply Add new rows onto the end. So... DataSet.Table.Rows.Add(NewRow); will add a new row to that table.

The SqlConnection commands are used when you have another type of data source. You can put SQL Queries to an SQL Connection. They are under the System.Data.SqlClient namespace

Edit: LINQ stands for Language-INtegrated-Query. It's almost like performing SQL commands on objects in your program. LINQ can be used in many situations. For example; let's say I had a List of type MyClass and MyClass has a property called MyInt of Int32. The list is called myList and I wanted to get all the entries that had MyInt set to "15"

var myResults = from resultSet in intList
                where resultSet.MyInt == 15
                select resultSet;

This would return an IEnumerable of type MyClass.

Let's do that again, but this time MyClass has a String type called MyString and I ONLY want the contents of MyString where MyInt is 15.

var myResults = from resultSet in intList
                where resultSet.MyInt == 15
                select resultSet.MyString;

would return an IEnumerable of type String containing all the MyStrings where MyInt was equal to 15.

You can also use it with lamda expressions...

foreach(String myString in myList.Where(s => s.MyInt == 15))
{
   Console.WriteLine(myString);
}

It's very very powerful and extremely flexible.

For example; This Guy created a RayTracer using a single LINQ statement

This might be a long shot, but have you tried

using System.Data.SqlClient

That's where the SqlConnection type resides.

This might be a long shot, but have you tried

using System.Data.SqlClient

That's where the SqlConnection type resides.

Firstly, I did mention that already ;)

Secondly, as the OP's using DataSets I don't think they actually need SqlConnection as the DataSet should do all the work for them.

I've added the LINQ stuff in because they asked about it and it's also a valid an preferable way forward with Database interactions depending on the scale and complexity of the OP's requirements. =)

Wow, am I blind. I completely missed that >< Sorry about that.

>The data source that i used was "Microsoft SQL Server Compact 3.5 (.NET Framework Data Provider for Microsoft SQL Server Compact 3.5)" and it was stored on my computer.

Paul, you need to learn database integration class library first (Data Provider classes of ADO.NET). Data providers are used for connecting to a database, executing commands, and retrieving results.

For Microsoft SQL Server Compact database (aka Microsoft Embedded Database), System.Data.SqlServerCe data provider is used (We need to add the reference of System.Data.SqlServerCe.dll)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.