473,396 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help in using queries from MS Access in Visual C# .NET

9
Hello,
I'm working on a little project that has to get into a database on a server. My question is: How am I supposed to use the queries I made in MS Access in my C# project? I'm talking about queries such as:

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS IDExists
  2. FROM tblCustomer
  3. WHERE ID=[@ID];

How am I supposed to send parameters (such as "@ID") from my C# code into the query?
I mean, if I want to use the query as a function and put it in an if, for example: if(storedProcedureCount>0)
Please help, I'm a beginner as you see, I will appreciate it if you explain me in detail.
With thanks, itsme7
Sep 25 '07 #1
2 1271
hbxtlhx
10
Hello,
I'm working on a little project that has to get into a database on a server. My question is: How am I supposed to use the queries I made in MS Access in my C# project? I'm talking about queries such as:

Expand|Select|Wrap|Line Numbers
  1. SELECT Count(*) AS IDExists
  2. FROM tblCustomer
  3. WHERE ID=[@ID];

How am I supposed to send parameters (such as "@ID") from my C# code into the query?
I mean, if I want to use the query as a function and put it in an if, for example: if(storedProcedureCount>0)
Please help, I'm a beginner as you see, I will appreciate it if you explain me in detail.
With thanks, itsme7
such as:

string queryString = "SELECT Count(1) AS IDExists FROM tblCustomer HERE ID=?";
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a";
object retValue= command.ExecuteScalar();
if (retValue is int && (int)retValue>0)
{
//exists records
}
else
{
//not exists
}
Sep 26 '07 #2
itsme7
9
such as:

string queryString = "SELECT Count(1) AS IDExists FROM tblCustomer HERE ID=?";
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a";
object retValue= command.ExecuteScalar();
if (retValue is int && (int)retValue>0)
{
//exists records
}
else
{
//not exists
}
Mmm OK, just a couple questions:
1. Why did you change the * in count to 1?
2. How do I manage the connections, I mean, what am I supposed to write in the code in order to make a connection?
3. Is the "a" a parameter (I mean, is it the @ID)?
4. What does the parameter OleDbType.Char and 3 mean?
5. What if I have more than 1 parameter? like in the following query:
Expand|Select|Wrap|Line Numbers
  1.  SELECT WasUsed
  2. FROM tblCustomer
  3. WHERE CPUCode=[@CPUCode];
6. Why did you put a question mark instead of [@ID]?

Oh and another thing: the Value in line command.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a"; doesn't pop up when I write the code. I am using Visual Studios .NET 2003.
Sep 26 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Job Lot | last post by:
I have written 6 Queries in an Access DB, which are executed in a For Each…Next loop to populate DataSet object. A new DataTable object is created in DataSet for each query, as follows Private...
1
by: Frank | last post by:
Aloha, I have a DB in Access 2000 consisting of about 1500 lines of code and 65 tables, 95 queries and 60 forms and want to "merge" with another persons program written in Access 2 with about the...
9
by: Dom Boyce | last post by:
Hi First up, I am using MS Access 2002. I have a database which records analyst rating changes for a list of companies on a daily basis. Unfortunately, the database has been set up (by my...
8
by: Tcs | last post by:
I've been stumped on this for quite a while. I don't know if it's so simple that I just can't see it, or it's really possible. (Obviously, I HOPE it IS possible.) I'm trying to get my queries...
5
by: meyvn77 | last post by:
I wrote a bunch of SQL statement in SQL SERVER and now im trying to do the same thing in access. OMG what a pain that Access can't handle updates or CAST() the way SQL server can. OK I thought I...
32
by: robert d via AccessMonster.com | last post by:
I'm looking at converting DAO to ADO in my app. All of my DAO connections are of the following structure: Dim wsName As DAO.Workspace Dim dbName As DAO.Database Dim rsName As DAO.Recordset ...
18
by: Ann Scharpf via AccessMonster.com | last post by:
I am not sure which would be the best place to post this question, so I'm posing it here with Access general questions. I have reached the point many times in Word and in Access where my ignorance...
5
by: Anns via AccessMonster.com | last post by:
My establishment has about 20 ms access db's that will be converted over (see subject). When we pull all the BE's over to SQL and the FE's on Sharepoint (.net) surely we don't have to change...
3
by: mcmahonb | last post by:
Hey people... I've been searching this forum for a few hours and even though this topic has been went over from many different angles; I cannot seem to figure out how to make things work on my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.