|
|
|
ขอสอบถามพี่ๆ ผมใช้ VS 2013 C#.Net เขียนเชื่อมต่อฐานข้อมูลเป็น postgresql ต้องใช้อะไรบ้างครับ |
|
|
|
|
|
|
|
Code (C#)
NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=postgres;" +
"Password=pwd;Database=postgres;");
conn.Open();
ที่เหลือก็เหมือน ๆ กันเลยครับ
|
|
|
|
|
Date :
2015-03-17 16:30:08 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Example (C#)
public void InitializeComponent()
{
this.ClientSize = new System.Drawing.Size(640, 400);
npgridfrm = new DataGrid();
npgridfrm.Location = new Point(10, 10);
npgridfrm.Size = new Size(620, 350);
this.Text = "Simple example of data grid by Npgsql";
this.Controls.Add(npgridfrm);
ok = new Button();
ok.Location = new Point(10, 365);
ok.Size = new Size(70, 25);
ok.TabIndex = 1;
ok.Text = "&Ok";
this.Controls.Add(ok);
ok.Click += new System.EventHandler(button_Click);
tipok = new ToolTip();
tipok.SetToolTip(ok, "Updated refreshing");
exit = new Button();
exit.Location = new Point(95, 365);
exit.Size = new Size(70, 25);
exit.TabIndex = 1;
exit.Text = "&Exit";
this.Controls.Add(exit);
exit.Click += new System.EventHandler(button_Click);
tipexit = new ToolTip();
tipexit.SetToolTip(exit, "End of this program");
ConnectToData();
// Here we connect Npgsql datasource "dtsource" created in previous method with our form.
try
{
npgridfrm.DataSource = dtsource;
npgridfrm.SetDataBinding(dset, "npdata");
dtsource.RowChanged += new DataRowChangeEventHandler(Row_Changed);
}
catch (Exception ex) { }
}
// This method is responsible to get the data from server and
// setup delete, update and insert commands for table.
public void ConnectToData()
{
string DSN;
Npgsql.Design.ConnectionStringEditorForm Ndesign = new Npgsql.Design.ConnectionStringEditorForm();
Ndesign.ShowDialog();
DSN = Ndesign.ConnectionString.ToString();
if (DSN == "")
return;
conn = new NpgsqlConnection(DSN);
dset = new DataSet("npdata");
NpAdapter = new NpgsqlDataAdapter();
NpAdapter.SelectCommand = new NpgsqlCommand(query, conn);
NpAdapter.Fill(dset, "npdata");
dtsource = dset.Tables["npdata"];
deleteCmd();
updateCmd();
insertCmd();
}
// Setups the delete command.
public void deleteCmd()
{
string query = "DELETE FROM npdata WHERE key = @key";
NpAdapter.DeleteCommand = new NpgsqlCommand(query, conn);
NpParam = NpAdapter.DeleteCommand.Parameters.Add("@key", NpgsqlTypes.NpgsqlDbType.Text);
NpParam.SourceColumn = "key";
NpParam.SourceVersion = DataRowVersion.Original;
}
// Setups the update command.
public void updateCmd()
{
string query = "UPDATE npdata SET key = @key, ndata = @ndata WHERE key = @key";
NpAdapter.UpdateCommand = new NpgsqlCommand(query, conn);
NpParam = NpAdapter.UpdateCommand.Parameters.Add("@key", NpgsqlTypes.NpgsqlDbType.Text);
NpParam.SourceColumn = "key";
NpParam.SourceVersion = DataRowVersion.Original;
NpParam = NpAdapter.UpdateCommand.Parameters.Add("@ndata", NpgsqlTypes.NpgsqlDbType.Bigint);
NpParam.SourceVersion = DataRowVersion.Current;
NpParam.SourceColumn = "ndata";
}
// Setups the insert command.
public void insertCmd()
{
string insertQuery = "INSERT INTO npdata VALUES (@key, @ndata)";
NpAdapter.InsertCommand = new NpgsqlCommand(insertQuery, conn);
NpParam = NpAdapter.InsertCommand.Parameters.Add("@key", NpgsqlTypes.NpgsqlDbType.Text);
NpParam.SourceColumn = "key";
NpParam.SourceVersion = DataRowVersion.Current;
NpParam = NpAdapter.InsertCommand.Parameters.Add("@ndata", NpgsqlTypes.NpgsqlDbType.Bigint);
NpParam.SourceVersion = DataRowVersion.Current;
NpParam.SourceColumn = "ndata";
}
|
|
|
|
|
Date :
2015-03-17 16:32:14 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|