1. I drag the sqlDataAdapter and sqlConnection from the
tool bar to the web form.
It creates automatically: -
protected System.Data.SqlClient.SqlDataAdapter
-
sqlDataAdapter1;
-
protected System.Data.SqlClient.SqlCommand
-
sqlSelectCommand1;
-
protected System.Data.SqlClient.SqlCommand
-
sqlInsertCommand1;
-
protected System.Data.SqlClient.SqlCommand
-
sqlUpdateCommand1;
-
protected System.Data.SqlClient.SqlCommand
-
sqlDeleteCommand1;
-
protected System.Data.SqlClient.SqlConnection
-
sqlConnection1;
-
-
override protected void OnInit(EventArgs e)
-
{
-
InitializeComponent();
-
base.OnInit(e);
-
}
-
-
private void InitializeComponent()
-
{
-
this.sqlDataAdapter1 = new
-
System.Data.SqlClient.SqlDataAdapter();
-
this.sqlSelectCommand1 = new
-
System.Data.SqlClient.SqlCommand();
-
this.sqlInsertCommand1 = new
-
System.Data.SqlClient.SqlCommand();
-
this.sqlUpdateCommand1 = new
-
System.Data.SqlClient.SqlCommand();
-
this.sqlDeleteCommand1 = new
-
System.Data.SqlClient.SqlCommand();
-
this.sqlConnection1 = new
-
System.Data.SqlClient.SqlConnection();
-
this.Button1.Click += new System.EventHandler
-
(this.Button1_Click);
-
-
this.sqlDataAdapter1.DeleteCommand =
-
this.sqlDeleteCommand1;
-
this.sqlDataAdapter1.InsertCommand =
-
this.sqlInsertCommand1;
-
this.sqlDataAdapter1.SelectCommand =
-
this.sqlSelectCommand1;
-
this.sqlDataAdapter1.TableMappings.AddRange(new
-
System.Data.Common.DataTableMapping[] {
-
new System.Data.Common.DataTableMapping
-
("Table", "phonebook", new
-
System.Data.Common.DataColumnMapping[] {
-
new System.Data.Common.DataColumnMapping
-
("id", "id"),
-
new System.Data.Common.DataColumnMapping
-
("name", "name"),
-
new System.Data.Common.DataColumnMapping
-
("phone", "phone"),
-
-
this.sqlDataAdapter1.UpdateCommand =
-
this.sqlUpdateCommand1;
-
-
this.sqlSelectCommand1.CommandText = "SELECT id,
-
name, phone FROM dbo.phonebook";
-
this.sqlSelectCommand1.Connection =
-
this.sqlConnection1;
-
-
this.sqlInsertCommand1.CommandText = "INSERT INTO
-
dbo.phonebook(name, phone) VALUES (@name, @phone); SELECT
-
id, name, phone FROM dbo.phonebook WHERE (id =
-
@@IDENTITY)";
-
-
this.sqlInsertCommand1.Connection =
-
this.sqlConnection1;
-
this.sqlInsertCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@name",
-
System.Data.SqlDbType.NVarChar, 20, "name"));
-
this.sqlInsertCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@phone",
-
System.Data.SqlDbType.NVarChar, 10, "phone"));
-
-
this.sqlUpdateCommand1.CommandText = @"UPDATE
-
dbo.phonebook SET name = @name, phone = @phone WHERE (id =
-
@Original_id) AND (phone = @Original_phone); SELECT id,
-
name, phone FROM dbo.phonebook WHERE (id = @id)";
-
-
this.sqlUpdateCommand1.Connection =
-
this.sqlConnection1;
-
this.sqlUpdateCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@name",
-
System.Data.SqlDbType.NVarChar, 20, "name"));
-
this.sqlUpdateCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@phone",
-
System.Data.SqlDbType.NVarChar, 10, "phone"));
-
-
this.sqlUpdateCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@Original_id",
-
System.Data.SqlDbType.Int, 4,
-
System.Data.ParameterDirection.Input, false, ((System.Byte)
-
(0)), ((System.Byte)(0)), "id",
-
System.Data.DataRowVersion.Original, null));
-
-
this.sqlUpdateCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@Original_phone",
-
System.Data.SqlDbType.NVarChar, 160,
-
System.Data.ParameterDirection.Input, false, ((System.Byte)
-
(0)), ((System.Byte)(0)), "phone",
-
System.Data.DataRowVersion.Original, null));
-
this.sqlUpdateCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@Original_name",
-
System.Data.SqlDbType.NVarChar, 1000,
-
System.Data.ParameterDirection.Input, false, ((System.Byte)
-
(0)), ((System.Byte)(0)), "name",
-
System.Data.DataRowVersion.Original, null));
-
this.sqlUpdateCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@id",
-
System.Data.SqlDbType.Int, 4, "id"));
-
-
-
this.sqlDeleteCommand1.CommandText = "DELETE FROM
-
dbo.phonebook WHERE (id = @Original_id) AND (phone =
-
@Original_phone)";
-
this.sqlDeleteCommand1.Connection =
-
this.sqlConnection1;
-
this.sqlDeleteCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@Original_id",
-
System.Data.SqlDbType.Int, 4,
-
System.Data.ParameterDirection.Input, false, ((System.Byte)
-
(0)), ((System.Byte)(0)), "id",
-
System.Data.DataRowVersion.Original, null));
-
this.sqlDeleteCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@Original_phone",
-
System.Data.SqlDbType.NVarChar, 160,
-
System.Data.ParameterDirection.Input, false, ((System.Byte)
-
(0)), ((System.Byte)(0)), "phone",
-
System.Data.DataRowVersion.Original, null));
-
this.sqlDeleteCommand1.Parameters.Add(new
-
System.Data.SqlClient.SqlParameter("@Original_name",
-
System.Data.SqlDbType.NVarChar, 1000,
-
System.Data.ParameterDirection.Input, false, ((System.Byte)
-
(0)), ((System.Byte)(0)), "name",
-
System.Data.DataRowVersion.Original, null));
-
-
this.sqlConnection1.ConnectionString = "data
-
source=SERVER;initial catalog=phonedb;persist security
-
info=False;user id=phoneuser;password:useruser;workstation
-
id=SERVER;packet size=4096";
-
this.Load += new System.EventHandler
-
(this.Page_Load);
-
}
-
-
private void Button1_Click(object sender, System.EventArgs
-
e)
-
{
-
}
-
Although insert, delete, update and select sql statements
were done automatically, however, how can i make use of
them? i.e. how can I call them to use under Button1_Click?
2. You can see the this.sqlConnection1.ConnectionString
= "data source=SERVER;initial catalog=phonedb;persist
security info=False;user
id=phoneuser;password:useruser;workstation
id=SERVER;packet size=4096";
It has to include the login id and password in order to
make such connection. I guess it is not secure. How should
I write a secure dbconnection code? Am I better to write a
new DBConnect class?
If I use my DBConnect class, does the above auto-generated
code not useful?
Thanks for advice. 2 1491
Tom,
In order to call the data adatper in your click event handler, you can
just call to sqlDataAdapter1. It's a member of your class, like any other
member. So, if you wanted to fill a dataset with the results from the
query, you would do:
// Fill the data set.
DataSet pobjDataSet = new DataSet();
sqlDataAdapter1.Fill(pobjDataSet);
As for creating a new connection class, that's not necessary. You have
one of two options here. The first is to store the connection information
in another location which is more secure. The second is to use trusted
connections (windows security) and not have to worry about logging in at
all.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Tom" <ke*****@yahoo.com> wrote in message
news:09****************************@phx.gbl... 1. I drag the sqlDataAdapter and sqlConnection from the tool bar to the web form.
It creates automatically:
- protected System.Data.SqlClient.SqlDataAdapter
- sqlDataAdapter1;
- protected System.Data.SqlClient.SqlCommand
- sqlSelectCommand1;
- protected System.Data.SqlClient.SqlCommand
- sqlInsertCommand1;
- protected System.Data.SqlClient.SqlCommand
- sqlUpdateCommand1;
- protected System.Data.SqlClient.SqlCommand
- sqlDeleteCommand1;
- protected System.Data.SqlClient.SqlConnection
- sqlConnection1;
- override protected void OnInit(EventArgs e)
- {
- InitializeComponent();
- base.OnInit(e);
- }
- private void InitializeComponent()
- {
- this.sqlDataAdapter1 = new
- System.Data.SqlClient.SqlDataAdapter();
- this.sqlSelectCommand1 = new
- System.Data.SqlClient.SqlCommand();
- this.sqlInsertCommand1 = new
- System.Data.SqlClient.SqlCommand();
- this.sqlUpdateCommand1 = new
- System.Data.SqlClient.SqlCommand();
- this.sqlDeleteCommand1 = new
- System.Data.SqlClient.SqlCommand();
- this.sqlConnection1 = new
- System.Data.SqlClient.SqlConnection();
- this.Button1.Click += new System.EventHandler
- (this.Button1_Click);
- this.sqlDataAdapter1.DeleteCommand =
- this.sqlDeleteCommand1;
- this.sqlDataAdapter1.InsertCommand =
- this.sqlInsertCommand1;
- this.sqlDataAdapter1.SelectCommand =
- this.sqlSelectCommand1;
- this.sqlDataAdapter1.TableMappings.AddRange(new
- System.Data.Common.DataTableMapping[] {
- new System.Data.Common.DataTableMapping
- ("Table", "phonebook", new
- System.Data.Common.DataColumnMapping[] {
- new System.Data.Common.DataColumnMapping
- ("id", "id"),
- new System.Data.Common.DataColumnMapping
- ("name", "name"),
- new System.Data.Common.DataColumnMapping
- ("phone", "phone"),
- this.sqlDataAdapter1.UpdateCommand =
- this.sqlUpdateCommand1;
- this.sqlSelectCommand1.CommandText = "SELECT id,
- name, phone FROM dbo.phonebook";
- this.sqlSelectCommand1.Connection =
- this.sqlConnection1;
- this.sqlInsertCommand1.CommandText = "INSERT INTO
- dbo.phonebook(name, phone) VALUES (@name, @phone); SELECT
- id, name, phone FROM dbo.phonebook WHERE (id =
- @@IDENTITY)";
- this.sqlInsertCommand1.Connection =
- this.sqlConnection1;
- this.sqlInsertCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@name",
- System.Data.SqlDbType.NVarChar, 20, "name"));
- this.sqlInsertCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@phone",
- System.Data.SqlDbType.NVarChar, 10, "phone"));
- this.sqlUpdateCommand1.CommandText = @"UPDATE
- dbo.phonebook SET name = @name, phone = @phone WHERE (id =
- @Original_id) AND (phone = @Original_phone); SELECT id,
- name, phone FROM dbo.phonebook WHERE (id = @id)";
- this.sqlUpdateCommand1.Connection =
- this.sqlConnection1;
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@name",
- System.Data.SqlDbType.NVarChar, 20, "name"));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@phone",
- System.Data.SqlDbType.NVarChar, 10, "phone"));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_id",
- System.Data.SqlDbType.Int, 4,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "id",
- System.Data.DataRowVersion.Original, null));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_phone",
- System.Data.SqlDbType.NVarChar, 160,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "phone",
- System.Data.DataRowVersion.Original, null));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_name",
- System.Data.SqlDbType.NVarChar, 1000,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "name",
- System.Data.DataRowVersion.Original, null));
- this.sqlUpdateCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@id",
- System.Data.SqlDbType.Int, 4, "id"));
- this.sqlDeleteCommand1.CommandText = "DELETE FROM
- dbo.phonebook WHERE (id = @Original_id) AND (phone =
- @Original_phone)";
- this.sqlDeleteCommand1.Connection =
- this.sqlConnection1;
- this.sqlDeleteCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_id",
- System.Data.SqlDbType.Int, 4,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "id",
- System.Data.DataRowVersion.Original, null));
- this.sqlDeleteCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_phone",
- System.Data.SqlDbType.NVarChar, 160,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "phone",
- System.Data.DataRowVersion.Original, null));
- this.sqlDeleteCommand1.Parameters.Add(new
- System.Data.SqlClient.SqlParameter("@Original_name",
- System.Data.SqlDbType.NVarChar, 1000,
- System.Data.ParameterDirection.Input, false, ((System.Byte)
- (0)), ((System.Byte)(0)), "name",
- System.Data.DataRowVersion.Original, null));
- this.sqlConnection1.ConnectionString = "data
- source=SERVER;initial catalog=phonedb;persist security
- info=False;user id=phoneuser;password:useruser;workstation
- id=SERVER;packet size=4096";
- this.Load += new System.EventHandler
- (this.Page_Load);
- }
- private void Button1_Click(object sender, System.EventArgs
- e)
- {
- }
-
Although insert, delete, update and select sql statements were done automatically, however, how can i make use of them? i.e. how can I call them to use under Button1_Click?
2. You can see the this.sqlConnection1.ConnectionString = "data source=SERVER;initial catalog=phonedb;persist security info=False;user id=phoneuser;password:useruser;workstation id=SERVER;packet size=4096";
It has to include the login id and password in order to make such connection. I guess it is not secure. How should I write a secure dbconnection code? Am I better to write a new DBConnect class?
If I use my DBConnect class, does the above auto-generated code not useful?
Thanks for advice.
Dear Sir,
Thank you for your kind help. Since I am a newbie and just
read some ASP.net and C# books. I am not still familiar
with it.
Here are my questions:
1. Do I write the code like that?
private void Button1_Click(object sender, System.EventArgs
e)
{
DataSet pobjDataSet = new DataSet();
sqlDataAdapter1.Fill(pobjDataSet);
}
But, how can I call the insert command? I need to input
the textbox to the DB.
2. The first is to store the connection information in
another location which is more secure.
Do you mean store it in the registry? How can I call it
from the code after I save it in registry?
The second is to use trusted connections (windows
security) and not have to worry about logging in at
all.
If I use windows security (I guess it is better), how
should I write the following?
this.sqlConnection1.ConnectionString = "data
source=SERVER;initial catalog=phonedb;persist security
info=False;user id=phoneuser;password:useruser;workstation
id=SERVER;packet size=4096";
Regards
Tom -----Original Message----- Tom,
In order to call the data adatper in your click event
handler, you canjust call to sqlDataAdapter1. It's a member of your
class, like any othermember. So, if you wanted to fill a dataset with the
results from thequery, you would do:
// Fill the data set. DataSet pobjDataSet = new DataSet(); sqlDataAdapter1.Fill(pobjDataSet);
As for creating a new connection class, that's not
necessary. You haveone of two options here. The first is to store the
connection informationin another location which is more secure. The second is
to use trustedconnections (windows security) and not have to worry
about logging in atall.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: softwareengineer2006 |
last post by:
All Interview Questions And Answers
10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP)
I have listed over 10000 interview questions asked
in interview/placement test papers for...
|
by: connectrajesh |
last post by:
INTERVIEWINFO.NET
http://www.interviewinfo.net
FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES
NO ADVERTISEMENT
|
by: freepdfforjobs |
last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions
http://www.questpond.com/SampleInterviewQuestionBook.zip
Download the JAVA , .NET and SQL Server interview sheet and rate...
|
by: Drew |
last post by:
I posted this to the asp.db group, but it doesn't look like there is much
activity on there, also I noticed that there are a bunch of posts on here
pertaining to database and asp. Sorry for...
|
by: Krypto |
last post by:
Hi,
I have used Python for a couple of projects last year and I found it
extremely useful. I could write two middle size projects in 2-3 months
(part time). Right now I am a bit rusty and trying...
|
by: ramu |
last post by:
C# Interview Questions and Answers8
http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html
C# Interview Questions and Answers7...
|
by: ramu |
last post by:
C# Interview Questions and Answers8
http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html
C# Interview Questions and Answers7...
|
by: ramu |
last post by:
C# Interview Questions and Answers8
http://allinterviewsbooks.blogspot.com/2008/07/c-interview-questions-and-answers8.html
C# Interview Questions and Answers7...
|
by: reema |
last post by:
EJB Interview Questions
http://interviewdoor.com/technical/EJB-Interview-Questions.htm
CSS Interview Questions
http://interviewdoor.com/technical/CSS-Interview-Questions.htm
C Interview Questions...
|
by: reema |
last post by:
EJB Interview Questions
http://interviewdoor.com/technical/EJB-Interview-Questions.htm
CSS Interview Questions
http://interviewdoor.com/technical/CSS-Interview-Questions.htm
C Interview Questions...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |