Sie sind auf Seite 1von 5

.

net and SQLserver Database connection :( visual studio 2005)


1. Create a form
2. right click on project in solution explorer-> add -> new item-> SQL dataase
Sa!e "s #select the path$
%. go to &ata->add data src-> select SQLser!er dataase file->select dataase-> test connection-> finish
'. Create tales and sa!e #eg. info # id numeric ->prim ke( ) name !archar$
*. &en write the following code to add records to dataase
C# code
using S(stem.Configuration+
using S(stem.&ata.S,lClient+
namespace -indows"pplication1
.
pulic partial class /orm1 0 /orm
.
S,lConnection con+
S,lCommand cmd+
string ,uer(+
pulic /orm1#$
.
con 1 new S,lConnection#23 &ataSource1.4SQL56785SS+"ttach&/ilename1C04&ocuments and
Settings49ithila49( &ocuments4:isual Studio
2;;*47rojects4-indows"pplication14-indows"pplication14&ataase1.mdf+<ntegrated Securit(1=rue+Connect
=imeout1%;+>ser <nstance1=rue3$+
<nitiali?eComponent#$+

@
pri!ate !oid utton1AClick#oject sender) 5!ent"rgs e$
.


con.Bpen#$+

,uer( 1 3<CS58= <C=B info :"L>5S #3 D textEox1.=extD3)F3D textEox2.=extD3F$+3+
cmd 1 new S,lCommand#,uer() con$+
GHH other wa( is0
H cmd 1 new S,lCommand#$+
cmd.Connection 1 con+
cmd.Command=ext 1 ,uer(+ HG
int temp 1 cmd.5xecuteConQuer(#$+
if #temp > ;$
.
9essageEox.Show#38ecord "dded3$+
@
else
.
9essageEox.Show#38ecord not "dded3$+
@

con.Close#$+



@
pri!ate !oid exitAClick#oject sender) 5!ent"rgs e$
.
this.Close#$+
@
@
@
VB.net code
Imports System.Data.SqlClient
Public Class Form1
Dim con1 As SQLConnection
Dim cmd As SQLCommand
Dim query As String
Private Sub utton1!Clic"#y$al sender As System.%b&ect' y$al e As System.(ventArgs)
*andles utton1.Clic"
con1 + ,e- SQLConnection#.DataSource+./SQL(0P1(SS2Attac3DbFilename+C4/Documents and
Settings/5it3ila/5y Documents/$isual Studio
6778/Pro&ects/9indo-sApplication1/9indo-sApplication1/Database1.md:2Integrated
Security+;rue2Connect ;imeout+<72=ser Instance+;rue.)
con1.%pen#)
query + .insert into in:o values#>. ? $al#;e@to@1.;e@t) ? .>'>. ? ;e@to@6.;e@t ?
.>).
cmd + ,e- SQLCommand#query' con1)
cmd.Command;ype + Command;ype.;e@t
cmd.(@ecute,onQuery#)
5sgo@#. Inserted.)
(nd Sub
(nd Class
.net and Oracle 0! Database connection :( visual studio 2005)
8ef0 http0GGwww.oracle.comGtechnolog(GpuGarticlesGcookAdotnet.html
sername4systemAscott
pass-ord4oracleAtiger
1.add re:BC c4Aoracle@eAappAoracleAproductA17.6.7AserverAbinA%racleDataAccess.ddl
6.dataBCadd data sourceBCdatabaseBCconnection stringD
<.toolsBCconnect to databaseBCadd connectionBCmention server nameBCusername'pass-ord
C# code
using oracle.dataAccess.client2
%racleConnection con1 + ne- %racleConnection#.Data Source+@e2=ser
ID+system2pass-ord+oracle.)2
con1.%pen#)2
string sql1 + .insert into students values#>.Dte@to@1.;e@tD.>).2
%racleCommand cmd1 + ne- %racleCommand#sql1'con1)2
cmd1.Command;ype + Command;ype.;e@t2
cmd1.(@ecute,onQuery#)2
VB.net code
Imports %racle.DataAccess.Client
> oracle.dataAccess.client2
Public Class Form1
Dim con1 As %racleConnection
Dim cmd As %racleCommand
Dim query As String
Private Sub utton1!Clic"#y$al sender As System.%b&ect' y$al e As System.(ventArgs)
*andles utton1.Clic"
con1 + ,e- %racleConnection#.Data Source+0(2=ser ID+system2Pass-ord+oracle.)
con1.%pen#)
query + .insert into in:o values#>. ? $al#;e@to@1.;e@t) ? .>'>. ? ;e@to@6.;e@t ?
.>).
cmd + ,e- %racleCommand#query' con1)
cmd.Command;ype + Command;ype.;e@t
cmd.(@ecute,onQuery#)
5sgo@#. Inserted.)
(nd Sub
(nd Class

Das könnte Ihnen auch gefallen