12 Replies - 4613 Views - Last Post: 26 August 2011 - 03:08 PM Rate Topic: -----

#1 impactblu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-August 11

ODBC with MS SQL in C#

Posted 26 August 2011 - 08:50 AM

Hi I'm new to C# and new to ODBC and my programming background is kinda weak.
I've been googling the errors in the debug menu and trying to fix them but they just keep creating more problems.

this is my program with the least amount of errors so far:




using System.Data.SqlClient;
using System.Configuration;

{
SqlConnection myConnection = new SqlConnection("user id=example;" + 
                                       "password=example;server=BCSWS009;" + 
                                       "Trusted_Connection=yes;" + 
                                       "database=example; " + 
                                       "connection timeout=30");
try
{
    myConnection.Open();
}
catch(Exception e)
{
    Console.WriteLine(e.ToString());
}


SqlCommand myCommand = new SqlCommand("Command String", myConnection);
SqlCommand myCommand = new SqlCommand("INSERT INTO table (Column1, Column2) " +
                                     "Values ('string', 1)", myConnection);
myCommand.ExecuteNonQuery();

try
{
    SqlDataReader myReader = null;
    SqlCommand    myCommand = new SqlCommand("select * from table", 
                                             myConnection);
    myReader = myCommand.ExecuteReader();
    while(myReader.Read())
    {
        Console.WriteLine(myReader["Column1"].ToString());
        Console.WriteLine(myReader["Column2"].ToString());
    }
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
}
try
{
    myConnection.Close();
}
catch(Exception e)
{
    Console.WriteLine(e.ToString());
}



Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: ODBC with MS SQL in C#

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14351
  • View blog
  • Posts: 57,547
  • Joined: 12-June 08

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 08:56 AM

Would you kindly tell us what the errors are, and what lines they are occurring on?
Was This Post Helpful? 0
  • +
  • -

#3 impactblu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-August 11

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 09:04 AM

oh sorry the errors are
Error 1 A namespace cannot directly contain members such as fields or methods L5 C1 odbc
Error 2 Expected class, delegate, enum, interface, or struct L6 C34 odbc
Error 3 Expected class, delegate, enum, interface, or struct s L21 C28 odbc
Error 4 Expected class, delegate, enum, interface, or struct L22 C28 odbc
Error 5 Expected class, delegate, enum, interface, or struct L29 C35 odbc
Error 6 Identifier expected L34 C36 odbc
Error 7 Identifier expected L35 C36 odbc
Error 8 Type or namespace definition, or end-of-file expected L36 C5 odbc
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 09:05 AM

First of all, you're all over the place. SqlClient is only for MS Sql Server, not MySql, and certainly not using ODBC.

http://msdn.microsof....sqlclient.aspx

Quote

The System.Data.SqlClient namespace is the.NET Framework Data Provider for SQL Server.





If you can avoid using ODBC, then by all means, avoid it. It's old, clunky, and we have far better tools.

Go here:

http://www.mysql.com...ucts/connector/

And download the ADO.NET connector for MySQL. Then go here:

http://dev.mysql.com...dotnet/#ADO.NET

And scroll down to the section "Developing Applications Using MySQL ADO.NET" and read the tutorial.

Try that, then come back.




One note: the code with the least errors isn't always the most correct.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 09:22 AM

Some more notes:

you're putting code directly inside a namespace. You need to have that in a class, and the code inside a method.
Was This Post Helpful? 0
  • +
  • -

#6 impactblu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-August 11

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 09:25 AM

sorry i keep thinking i'm using MySQL but i'm using MS SQL server 2008 R2 could you direct me to those references for sql server

MOD EDIT: Edited title to reflect reality, as it currently stands.
Was This Post Helpful? 0
  • +
  • -

#7 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 11:19 AM

It looks like you copied the code from a website, pasted it into Visual Studio, compiled, saw errors and posted it here.

Notice how your code is just pasted inside of the namespaces area, not even in class or methods within a class.

This post has been edited by Sergio Tapia: 26 August 2011 - 11:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 impactblu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-August 11

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 11:26 AM

@sergio

I was following a tutorial on connecting to sql server with C# and my background in programming is very weak and i'm mostly dealt with C++ and at a level where I was writing

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
etc.
return 0;
}



As well I've only ever coded math programs i.e. programs to do matrix multiplication and the last thing I learned was object oriented programming contructors destructors but I barely dabbled in it I'm hoping that gives you an idea of what I'm working with.

I also look up the errors from the debug menu and try to figure out what i'm doing wrong but like I said my background is weak. My basics are also very weak so certain common knowledge terms I may not know

This post has been edited by impactblu: 26 August 2011 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 12:18 PM

Ok. If your background is that weak, I suggest that you not attempt something advanced like this. Start at the beginning. Work your way through a book or a tutorial series, since there are some core concepts that are very, very different from C++, and you need to learn them before you try doing things like ADO.NET. We all have to start somewhere, but the beginning is usually the right place.
Was This Post Helpful? 1
  • +
  • -

#10 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 12:44 PM

Quote

last thing I learned was object oriented programming contructors destructors but I barely dabbled in it I'm hoping that gives you an idea of what I'm working with.


Now this let's us suggest better alternatives.

You're just starting out, you shouldn't worry about data or xml or threads or anything like that at this point.

Buy this book, and read it from cover to cover.
http://www.amazon.co...k/dp/1430235306
Posted Image

It's only 378 pages long, should take you a week if you take your time right and solve the exercises. When you're done with this book I guarantee that you'll become better and have a clearly picture. It's just how it works, you have to run through the paces and understand what the compiler is doing when it runs your lines of code.

After you're done with that book, get this one. Another favorite of mine I suggest for newbies to C#.

Head First C#
http://oreilly.com/c...g/9780596514822
Posted Image

With those two books conquered not only will you have a better grasp of C# as a language, but you'll also become a bit more familiar with the GUI framework called Windows Forms.
Was This Post Helpful? 1
  • +
  • -

#11 impactblu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-August 11

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 01:21 PM

well this is sort of a final project for my work term as well
essentially what I'm doing is using a library created by a company dealing in automation which allows me to take "User-Defined Tags"(UDT's) read them and what I want to do is have them write to a database. By using their help file and some of the sample code provided I was able to get it to read the UDT's and output in treeview.

I've already created my SQL Server and the last bit I've been working on is getting the connection to the database as well as writing to the database. I was planning on going through head first C# this weekend because I'm ambitious If you have any resources that you could throw my way other than the books which I'm going to try to finish this weekend I would really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#12 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 02:02 PM

Unfortunately that's the only reasources I can recommend for you at this juncture. Your skill level is very low and you need this foundation in order to code up a program. Moreso a program that interacts with a database! :)

If you really want a crash course into C#/Winforms development, watch this video series. But remember, you will basically be coding on duct tape. Nothing substantial!

http://windowsclient...arn/videos.aspx - I highly discourage you from doing this!
Was This Post Helpful? 1
  • +
  • -

#13 impactblu   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 26-August 11

Re: ODBC with MS SQL in C#

Posted 26 August 2011 - 03:08 PM

thanks a lot I'm going to try my best to get through both books this weekend
appreciate the help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1