- 积分
- 40165
- 好友
- 记录
- 主题
- 帖子
- 听众
- 收听
|
发表于 2013-4-7 08:05:18
|
显示全部楼层
这个用C#的不多,用Java的到是很多,
[code=csharp]// Note: This C# example uses .NET Framework v1.1
// It connects to a Cloudscape/Derby database named SAMPLEDB.
// It creates a table called staff, inserts 3 rows, then fetches
// and displays the result set on the console.
using System;
using System.Data;
using System.Data.Odbc;
namespace IBM.Cloudscape.Samples
{
class Sample1
{
[STAThread]
static void Main(string[] args)
{
//String connString = @"Driver={IBM DB2 ODBC
//DRIVER};DBALIAS=SAMPLEDB;UID=abcWD=abc";
String connString = @"DSN=SAMPLEDB;UID=abcWD=abc"; // using DSN
OdbcConnection conn = null;
OdbcCommand cmd = null;
Console.WriteLine("Connecting to SAMPLEDB");
try
{
conn = new OdbcConnection(connString);
// open database connection
conn.Open();
Console.WriteLine("Connected to SAMPLEDB");
// creates a table named staff
cmd = new OdbcCommand("CREATE TABLE staff
(sid INT PRIMARY KEY NOT NULL, name VARCHAR(30)
NOT NULL, salary INT NOT NULL)", conn);
cmd.ExecuteNonQuery();
Console.WriteLine("Table staff created");
// insert data to table staff
cmd.CommandText = "INSERT INTO staff VALUES (300, 'John', 35000)";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO staff VALUES (310, 'Mary', 55000)";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO staff VALUES (320, 'Tom', 35000)";
cmd.ExecuteNonQuery();
Console.WriteLine("3 rows inserted");
// Fetch data from table staff
cmd = new OdbcCommand("SELECT * FROM staff", conn);
OdbcDataReader rdr = cmd.ExecuteReader();
Console.WriteLine("Fetching rows from staff");
while (rdr.Read())
{
Console.Write(rdr.GetInt32(0));
Console.Write(" " + rdr.GetString(1) + " ");
Console.WriteLine(rdr.GetInt32(2));
}
rdr.Close();
}
catch (OdbcException ex)
{
int cnt = ex.Errors.Count;
for (int i=0; i < cnt; i++)
{
Console.WriteLine("Error #" + i + "\n" +
"Message: " + ex.Errors.Message + "\n" +
"Native: " + ex.Errors.NativeError.ToString() + "\n" +
"SQL: " + ex.Errors.SQLState + "\n");
}
}
finally
{
if (conn != null)
conn.Close();
Console.WriteLine("Sample1 ended");
}
}
}
}[/code]
其实你直接使用OdbcConnection 这种方式就行了,写法应该是和Sql的一样的
只是换了连接名字
上面的代码仅做参考
|
|