SQL Server Management Studio - Beuth Hochschule für Technik

Werbung
Client/Server-Datenbank
0) Konzept
SQL Server Management Studio
Java-Programm
Datasource
Access-Oberfläche
Java Server Pages (Internet)
Datenbank
VB
C#
1) SQL Server Management Studio
2) Datenquelle
3) Java-Programm
import java.sql.*;
public class dbsqlserver
{ public static void main(String[] args) throws Exception
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:sqlserverdb2","student","tfhberlin");
Statement s = c.createStatement();
ResultSet r = s.executeQuery("SELECT * FROM person");
while (r.next())
{ System.out.println(r.getString("pnr") + " " + r.getString("pname"));
}
c.close();
}
}
4) Access-Oberfläche
5) Java Server Page
<html>
<body>
<br>
<h4 align="center">Erstellt am
<%= new java.util.Date().toLocaleString()%></h4>
<br>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c = DriverManager.getConnection("jdbc:odbc:sqlserverdb2","student","tfhberlin");
Statement stmt = c.createStatement();
final String query = "Select pnr, pname from person";
ResultSet rs = stmt.executeQuery(query);
%>
<table border="1" bgcolor="#CCFFF" cellpadding="3" cellspacing="0">
<tr>
<% int i = 1;
while (rs.next()) {%>
<tr><td align="center"><%= i%></td>
<td><%= rs.getString(1)%></td>
<td><%= rs.getString(2)%></td>
</tr>
<% i++;} %>
</table>
<% rs.close();
c.close();
%>
</body>
</html>
6) VB-Code
Imports system.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Dim cmd As String = "SELECT * FROM person"
Dim conn As New SqlConnection("server=localhost; database=vorlesung; User ID=student;
Password=tfh-berlin")
Dim sqlcmd As New SqlCommand(cmd, conn)
Dim dr As SqlDataReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
conn.Open()
dr = sqlcmd.ExecuteReader
Dim str As String
Do While dr.Read()
str = dr.Item("pnr") & ControlChars.Tab & dr.Item("pname")
ListBox1.Items.Add(str)
Loop
dr.Close()
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
7) C#-Code
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Text;
System.Windows.Forms;
System.Data.SqlClient;
System.Data.Odbc;
namespace person2
{
public partial class Form1 : Form
{
private DataSet ds;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string conStr = "Server=localhost; database=vorlesung; User
ID=student; Password=tfh-berlin";
SqlConnection conn = new SqlConnection(conStr);
ds = new DataSet();
string cmd = "SELECT * FROM person";
SqlDataAdapter da = new SqlDataAdapter(cmd, conn);
try
{
conn.Open();
da.Fill(ds,"Personen");
conn.Close();
DataTable dt = ds.Tables["Personen"];
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Herunterladen