473,802 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to enter data in a SQL database?

I want to create a simple user interface to collect the following data and
store the data in a SQL database…. Could someone please help me get started?

Data to collect from user interface and store in database:
tDateS (trade date sell)
tDateB (trade date buy)
contracts (number of contracts)
strike (stike price)
sValue (sell value)
bValue (buy value)

The follwing three values are calculated from the data above and stored in
database:
iToal (initial total)
pTotal (premium total)
pl (profit or loss)
I’m new to C# and don’t know where to begin… thank you,

Mark

Nov 17 '05 #1
3 3136
Hi Mark,
depending on what type of DB you are using the answer can be a little bit
different. By SQL Database do you mean Microsoft SQL Server?

In the example below I will assume a SQL Server DB. If you are not you
will need to swap SqlConnection for a OleDbConnection etc.

1. The first thing you need to know is the connection string to connect to
the DB. The connection sting contains things like the server name, database
name, user name, password etc (username and password might not always be
there depending on which security model you choose). A good place to find
the string you need is www.connectionstrings.com it shows examples for
different types of DBs. For example a SQL Server connection string could be:

string connectionStrin g = "Server=Aron1;D atabase=pubs;Us er
ID=sa;Password= asdasd;Trusted_ Connection=Fals e";

2. Next in .Net you need to create a Connection object to connect to the DB,
for SQL server this will be a System.Data.Sql Client.SqlConne ction:

SqlConnection connection = new SqlConnection(c onnectionString );

3. Next you have to say what type of SQL command are you going to use a
Stored procedure or a text expression i.e. "SELECT * FROM tblX". LEt's
assume you don't have any stored procedures (ideally you would), you need to
create a SqlCommand object:

SqlCommand command = new SqlCommand();
command.Command Type = CommandType.Tex t;
command.Command Text = "INSERT INTO tblData(a,b,c) VALUES(1,2,3)";
command.Connect ion = connection;

4. Open the connection and execute the command, the ExecuteNonQuery () method
indicates we don't expect any data coming back (unlike if we had done a
SELECT)

connection.Open ();
command.Execute NonQuery();

5. An important thing to do is to clean up after you have finished, you
don't want to leave open database connection lying around, so we want to
close the connection, you should always put your code inside a try{}
finally{} block with the connection.Clos e() inside the finally. An
alternative is to put it inside a using statement which will call Dispose
inside a finally behind the scenes which in turn calls Close. So you end up
with something like:

string connectionStrin g = "Server=Aron1;D atabase=pubs;Us er
ID=sa;Password= asdasd;Trusted_ Connection=Fals e";

using (SqlConnection connection = new SqlConnection(c onnectionString ))
using (SqlCommand command = new SqlCommand())
{
command.Command Type = CommandType.Tex t;
command.Command Text = "INSERT INTO tblData(a,b) VALUES(1,2)";
command.Connect ion = connection;
connection.Open ();

command.Execute NonQuery();
}
There are lots of excellent resources out there to look at, just search
google or look at the Microsoft help for SqlConnection etc and you will find
all you need.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"ma*********@ne wsgroups.nospam " wrote:
I want to create a simple user interface to collect the following data and
store the data in a SQL database…. Could someone please help me get started?

Data to collect from user interface and store in database:
tDateS (trade date sell)
tDateB (trade date buy)
contracts (number of contracts)
strike (stike price)
sValue (sell value)
bValue (buy value)

The follwing three values are calculated from the data above and stored in
database:
iToal (initial total)
pTotal (premium total)
pl (profit or loss)
I’m new to C# and don’t know where to begin… thank you,

Mark

Nov 17 '05 #2
For security purposes, you better use SqlParameter or OleDbParameter class
instead of construct the sqlcommand string directly from the user input,
this could avoid sql injection attack.

Cheers,
Ivan

"Mark R. Dawson" wrote:
Hi Mark,
depending on what type of DB you are using the answer can be a little bit
different. By SQL Database do you mean Microsoft SQL Server?

In the example below I will assume a SQL Server DB. If you are not you
will need to swap SqlConnection for a OleDbConnection etc.

1. The first thing you need to know is the connection string to connect to
the DB. The connection sting contains things like the server name, database
name, user name, password etc (username and password might not always be
there depending on which security model you choose). A good place to find
the string you need is www.connectionstrings.com it shows examples for
different types of DBs. For example a SQL Server connection string could be:

string connectionStrin g = "Server=Aron1;D atabase=pubs;Us er
ID=sa;Password= asdasd;Trusted_ Connection=Fals e";

2. Next in .Net you need to create a Connection object to connect to the DB,
for SQL server this will be a System.Data.Sql Client.SqlConne ction:

SqlConnection connection = new SqlConnection(c onnectionString );

3. Next you have to say what type of SQL command are you going to use a
Stored procedure or a text expression i.e. "SELECT * FROM tblX". LEt's
assume you don't have any stored procedures (ideally you would), you need to
create a SqlCommand object:

SqlCommand command = new SqlCommand();
command.Command Type = CommandType.Tex t;
command.Command Text = "INSERT INTO tblData(a,b,c) VALUES(1,2,3)";
command.Connect ion = connection;

4. Open the connection and execute the command, the ExecuteNonQuery () method
indicates we don't expect any data coming back (unlike if we had done a
SELECT)

connection.Open ();
command.Execute NonQuery();

5. An important thing to do is to clean up after you have finished, you
don't want to leave open database connection lying around, so we want to
close the connection, you should always put your code inside a try{}
finally{} block with the connection.Clos e() inside the finally. An
alternative is to put it inside a using statement which will call Dispose
inside a finally behind the scenes which in turn calls Close. So you end up
with something like:

string connectionStrin g = "Server=Aron1;D atabase=pubs;Us er
ID=sa;Password= asdasd;Trusted_ Connection=Fals e";

using (SqlConnection connection = new SqlConnection(c onnectionString ))
using (SqlCommand command = new SqlCommand())
{
command.Command Type = CommandType.Tex t;
command.Command Text = "INSERT INTO tblData(a,b) VALUES(1,2)";
command.Connect ion = connection;
connection.Open ();

command.Execute NonQuery();
}
There are lots of excellent resources out there to look at, just search
google or look at the Microsoft help for SqlConnection etc and you will find
all you need.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"ma*********@ne wsgroups.nospam " wrote:
I want to create a simple user interface to collect the following data and
store the data in a SQL database…. Could someone please help me get started?

Data to collect from user interface and store in database:
tDateS (trade date sell)
tDateB (trade date buy)
contracts (number of contracts)
strike (stike price)
sValue (sell value)
bValue (buy value)

The follwing three values are calculated from the data above and stored in
database:
iToal (initial total)
pTotal (premium total)
pl (profit or loss)
I’m new to C# and don’t know where to begin… thank you,

Mark

Nov 17 '05 #3
Agreed, I was not sure of Marks level of knowldege with SQL so I just put
straight SQL for the example, but you should definitely use stored
procedures of possible.

"Ivan Wong" wrote:
For security purposes, you better use SqlParameter or OleDbParameter class
instead of construct the sqlcommand string directly from the user input,
this could avoid sql injection attack.

Cheers,
Ivan

"Mark R. Dawson" wrote:
Hi Mark,
depending on what type of DB you are using the answer can be a little bit
different. By SQL Database do you mean Microsoft SQL Server?

In the example below I will assume a SQL Server DB. If you are not you
will need to swap SqlConnection for a OleDbConnection etc.

1. The first thing you need to know is the connection string to connect to
the DB. The connection sting contains things like the server name, database
name, user name, password etc (username and password might not always be
there depending on which security model you choose). A good place to find
the string you need is www.connectionstrings.com it shows examples for
different types of DBs. For example a SQL Server connection string could be:

string connectionStrin g = "Server=Aron1;D atabase=pubs;Us er
ID=sa;Password= asdasd;Trusted_ Connection=Fals e";

2. Next in .Net you need to create a Connection object to connect to the DB,
for SQL server this will be a System.Data.Sql Client.SqlConne ction:

SqlConnection connection = new SqlConnection(c onnectionString );

3. Next you have to say what type of SQL command are you going to use a
Stored procedure or a text expression i.e. "SELECT * FROM tblX". LEt's
assume you don't have any stored procedures (ideally you would), you need to
create a SqlCommand object:

SqlCommand command = new SqlCommand();
command.Command Type = CommandType.Tex t;
command.Command Text = "INSERT INTO tblData(a,b,c) VALUES(1,2,3)";
command.Connect ion = connection;

4. Open the connection and execute the command, the ExecuteNonQuery () method
indicates we don't expect any data coming back (unlike if we had done a
SELECT)

connection.Open ();
command.Execute NonQuery();

5. An important thing to do is to clean up after you have finished, you
don't want to leave open database connection lying around, so we want to
close the connection, you should always put your code inside a try{}
finally{} block with the connection.Clos e() inside the finally. An
alternative is to put it inside a using statement which will call Dispose
inside a finally behind the scenes which in turn calls Close. So you end up
with something like:

string connectionStrin g = "Server=Aron1;D atabase=pubs;Us er
ID=sa;Password= asdasd;Trusted_ Connection=Fals e";

using (SqlConnection connection = new SqlConnection(c onnectionString ))
using (SqlCommand command = new SqlCommand())
{
command.Command Type = CommandType.Tex t;
command.Command Text = "INSERT INTO tblData(a,b) VALUES(1,2)";
command.Connect ion = connection;
connection.Open ();

command.Execute NonQuery();
}
There are lots of excellent resources out there to look at, just search
google or look at the Microsoft help for SqlConnection etc and you will find
all you need.

Hope that helps
Mark R Dawson
http://www.markdawson.org

"ma*********@ne wsgroups.nospam " wrote:
I want to create a simple user interface to collect the following data and
store the data in a SQL database…. Could someone please help me get started?

Data to collect from user interface and store in database:
tDateS (trade date sell)
tDateB (trade date buy)
contracts (number of contracts)
strike (stike price)
sValue (sell value)
bValue (buy value)

The follwing three values are calculated from the data above and stored in
database:
iToal (initial total)
pTotal (premium total)
pl (profit or loss)
I’m new to C# and don’t know where to begin… thank you,

Mark

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3523
by: Susan | last post by:
I have a bound form and subform and am trying to enter the data in both forms programatically. The Linkmaster and Linkchild properties are set. My intent is to be able to look at the data before it is saved and then choose to save it or undo it. I have a Save button that just closes the form and a Cancel button that does an undo and then closes the form. I first enter data into the main form and then enter code into the subform. After the...
0
1713
by: Greg | last post by:
I am working on an application that requires working with numbers in scientific notation. I am using SqlServer as the database and I have created strongly typed data adapters and datasets. The numbers are defined as numeric in the SqlServer Database and are bound to text boxes in the app. In the dataset xml these numbers are defined as decimal. I am using the binding method Format to display the data in scientific notation and Parse to...
5
2076
by: fkater | last post by:
Hi, we need to enter lots of data which are basicly descriptions of pc hardware configuration like this: Mainboard: vendor=... #of PCI slots=3 slot 1 graphic adapter
2
8012
by: Ted Ngo | last post by:
Hi All, I did create a dropdownbox and populate the data from the database. I want the user able to enter the text or select data from the dropdownlist box. When the user enter the text and if it match the data in the dropdownlist box then those data will display. I want to do something like, in the IE, where the user enter the world wide web Address.
5
8237
by: Dakrat | last post by:
Allow me to preface this post by saying that this is my first database project, and while I have learned a lot, any concepts I have learned are hit and miss as I have found new requirements and researched solutions. That said, I have a "training" database with PowerPoint briefings which I have users access and complete training. The form then records the date and time they completed training in a relevant field. I have a separate "master"...
15
12178
by: Mr.Tom.Willems | last post by:
Hello people, I am ussing an MS access database to enter and manage data from lab tests. until now i was the only one handeling the data so i had no need for a controle on how missing data was entered, since i did it myself i knew exactly what data was missing... The problem is that i can have data that hasn't been enterd yet, or data that is below detection level(so missing).
4
3604
by: kveerendrareddy | last post by:
Hi friends, In my web page i have to create a combobox in HTML+JavaScript. The nature of that combobox should be such that it should allow the user to enter the data in the combobox dynamically(when page is opened) and as well as it should retrieve data from the database. With my code, it could retrieve data from the database, but it is allowing the user to enter data in combo box dynamically. Can anyone help me in...
10
4181
by: art | last post by:
Hi, I have a form with an image button like this: <td><input type='text' name='search' id='search' value='' size=30><BR><span class=body><center>Enter Email Address</span><BR></ td> <td><a href='#'><img src='./images/search.gif' border=0 onclick='return getCustomer(search.email.value);'></a><BR><BR></td>
16
8385
by: Greg (codepug | last post by:
If one converts that .mdb into an .mde the code is secure but the tables can still be imported. Just for Very Basic protection, I have placed a Password on the database using the "Set Database Password" option. Now it requires that the password be entered each time you start the database. How do I enter the Password using code, so that the database starts up without having to type it in ??? I notice that there is a way in code to set...
2
2173
by: Riak | last post by:
Hellow Helpers: I am doing data entry job manually and data is huge. Now I was told to write/get some sort of program/script which can do this job quickly, otherwise I will loose job. I am good computer user but far away from programming, however I can make minor/little changes into script/program e.g. if it reads one records, I can update record name to read or ask to read next record. This is all my knowledge/skills about programming. Here...
0
10529
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10301
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10280
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9107
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7596
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5620
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3788
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2964
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.