using&ms&sql&server&with&coldfusion&and&plesk&...

16
Using MS SQL Server with Coldfusion and Plesk Create an MSSQL database 1) Log in to plesk via the web. Be sure to type http otherwise it will not work: http://studentxx.upj.pitt.edu:8880 2) If using Internet Explorer, turn compatibility viewing mode on. 3) Click Databases 4) Click Add New Database. You may make up to five databases in your account. 5) Type in a database name. Choose the other options shown below: 6) Click Ok 7) Click Add New Database user

Upload: dangmien

Post on 11-Apr-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

Using  MS  SQL  Server  with  Coldfusion  and  Plesk  

Create  an  MSSQL  database  

1) Log  in  to  plesk  via  the  web.    Be  sure  to  type  http  otherwise  it  will  not  work:    http://studentxx.upj.pitt.edu:8880  

2) If  using  Internet  Explorer,  turn  compatibility  viewing  mode  on.  3) Click  Databases  4) Click  Add  New  Database.    You  may  make  up  to  five  databases  in  your  account.  5) Type  in  a  database  name.    Choose  the  other  options  shown  below:  

   

6) Click  Ok  7) Click  Add  New  Database  user  

 

8) Choose  a  username  and  password.    Choose  a  username  and  password.  If  it  gives  an  error,  you  may  have  to  choose  a  stronger  password.    Click  OK:  

 Your  database  is  now  created.      Connecting  to  the  database  using  Visual  Studio  Connect  to  the  database  using  Visual  Studio.    When  you  use  a  professional  database  (Oracle,  MSSQL,  etc),  you  are  accessing  the  database  on  a  server.        The  data  and  database  engine  is  on  a  remote  server.    You  will  be  using  client  tools  to  access  the  database.      The  client  tools  allow  you  to  design  tables,  relationships,  etc.    It  also  allows  you  to  edit  the  data  in  the  tables  as  well.      The  tools  that  can  be  used  for  MSSQL  are  either  MSSQL  Management  Studio  or  Visual  Studio.    I  am  doing  this  example  using  Visual  Studio  because  it  is  available  in  the  public  labs:    1)  Start  Visual  Studio.    If  you  need  to  pick  any  settings,  choose  general.    This  takes  a  long  time  

to  start  up  on  public  lab  machines.  2) You  do  not  need  to  create  a  project.  

3) Click  tools  -­‐>  Connect  to  Database:  

 

   

4) Adjust    fields  as  follows:        server  name:    sql1.upj.pitt.edu  Use    SQL  Authentication  Username:    [username  you  created  above]  Password:    [password  you  made  above]  Save  password:  Your  option  (you  have  to  keep  typing  it  in)  Select  or  enter  database:  [database  name  you  made  above]  

   

5) Click  Test  connection  and  you  should  get  a  successful  response.    Ok.  

6) Click  ok.    When  completed,  your  connection  to  the  db  will  show  in  the  server  explorer,  Data  connections.    If  this  window  ever  gets  hidden,    you  can  restore  it  with  CTRL-­‐ALT-­‐S.  

 7) At  this  point,  you  can  create  tables  and  relationships.  

 Creating  tables  and  relationships  in  MSSQL  with  Visual  Studio    

1) Right  click  tables  and  click  add  new  table.  

2) Add  the  fields  and  set  the  datatype  and  properties  as  needed.  

 3) I  am  using  and  autonumber  field  for  the  person  id.    To  do  so,  the  Identity  specification  is  set  

to  yes  and  the  identity  increment  is  set  to  1  and  seed  is  set  to  1.    You  can  also  click  the  key  

to  make  it  a  PK.  

 4) Save  the  table  and  give  it  a  name    (People).  

5) Next  I  make  a  state  table  with  the  following  fields.    Save  the  table:  

 6) Next  we  need  to  make  the  relationship  for  states.  

7) Open  one  of  the  tables  (People).      You  can  right  click  the  table  and  click  open  table  definition.    After  you  do  that,  you  get  the  table  icons  at  the  top:  

 8) Click  the  relationships  icon.  9) Click  add  in  the  dialog  box.  10) Expand  Tables  and  Column  Spec.  

11) Make  sure  Tables  and  Column  spec  is  highlighted  and  you  get  an  icon/button  with  …    Click  the  …  

 

12) Choose  the  two  tables  to  link.    Be  sure  to  choose  the  correct  table  for  the  PK  and  correct  table  for  the  FK:  

 13) Below,  click  the  box  and  a  pulldown  menu  will  magically  appear  to  choose  the  PK.  

 

14) Also  choose  the  FK  then  click  ok.    

 15) Click  Close.    Close  the  tables  also  so  that  the  change  commit.  

 Add  Data  to  tables:  

1) Right  click  a  table  and  click  show  table  data.  2) In  our  case,  we  will  need  to  fill  in  the  states  table  first.    There  will  be  some  latency  since  all  

data  resides  on    a  remote  server.  3) Records  are  not  committed  until  the  little  red  !  goes  away.    

     

4) Add  items  to  the  People  table  in  a  similar  fashion.  5) It  should  ensure  PF,  FK  relationships  are  followed.  

 6) Add  some  records.  

   Notes:  

1) You  can  work  on  the  db  and  web  code  at  the  same  time.    However,  the  database  client  only  sees  a  snapshot  of  the  data.    So  if  you  add  a  record  via  a  web  interface,  you  will  need  to  refresh  the  view  at  the  client  to  see  the  changes.  

2) I  changed  the  field  state  to  stateID  in  the  people  table.      

 

   

Creating  a  Coldfusion  DSN    (Applicable  for  Web  Programming  course):  In  order  for  ColdFusion  to  talk  to  the  database,  a  connection  must  be  made.    This  is  called  a  Data  Source  Name  Connection  (DSN  Connection).  

 1) In  order  to  access  the  database  via  coldfusion,  you  need  to  create  a  DSN  (Datasource  name).  2) Using  a  web  browser,  log  in  to  http://studentxx.upj.pitt.edu:8880.      3) Click  Coldfusion  DSN.  4) Click  Add  New  Coldfusion  DSN.  5) Type  in  a  unique  name  for  the  DSN.    (I  used  stu33MSSQL).  6) Choose  Microsoft  SQL  Server.  7) Click  Next  8) At  this  point  you  have  the  option  to  decide  if  you  want  to  add  a  username/pw.    If  you  add  it  

here,  you  don’t  need  to  have  it  in  your  source  code  but  your  application  will  be  less  secure:  

 9) When  you  Click  ok,  it  will  test  the  dsn  connection  and  you  should  get  a  green  checkmark:  

 10) You  can  now  use  this  connection  in  your  Coldfusion  code.  

 

 

Other  Tools  –  Testing  Queries  in  MSSQL  

1) You  can  test  queries  in  Visual  studio  using  .  2) Click  Data  –  New  Query.  3) Choose  your  tables  or  just  click  close.  4) You  can  check  the  syntax  and  execute  the  query  using  the  red  exclamation  point: