| |

VerySource

 Forgot password?
 Register
Search
View: 796|Reply: 9

How do windows applications connect to the database?

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-24 20:20:02
| Show all posts |Read mode
Brothers! Help!
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-11 22:00:01
| Show all posts
using System;
using System.Data;
using System.Data.SqlClient;

namespace news.common
{
 / **
  * -----------------
  * Database connection / operation class
  * ----------------
  * /

 public class DBClass
 {
  / * ---- Global variable definition ------ * /

  private SqlConnection conn;
  private SqlCommand comm;
  public SqlDataReader dr;
  public DataSet ds;
  public SqlDataAdapter dad;
  private string sql;
  private string connStr; / * database connection string * /
  private string errInfo = "";
  / * ---------------------- * /

  public DBClass ()
  {
  }
  / * Database operation exception information read-only attribute * /
  public string ErrInfo
  {
   get
   {
    return errInfo;
   }
  }
  / * Sql statement to operate * /
  public string Sql
  {
   get {
    return sql;
   }
   set {
    sql = value;
   }
  }
  / * Database link string * /
  public string ConnStr
  {
   get
   {
    return connStr;
   }
   set
   {
    connStr = value;
   }
  }
  private void connDb ()
  {
   conn = new SqlConnection (connStr);
   try
   {
    conn.Open ();
   }
   catch (SqlException e)
   {
    for (int i = 0; i <e.Errors.Count; i ++)
    {
     errInfo + = "Error serial number:" + i + "\n" +
                       "Error message:" + e.Errors [i] .Message + "\n" +
                       "Error source:" + e.Errors [i] .Source + "\n" +
                       "Procedure:" + e.Errors [i] .Procedure;
    }
    conn.Close ();
   }
  }
  / * For form binding * /
  public void dataView ()
  {
   connDb ();
   dad = new SqlDataAdapter (sql, conn);
   ds = new DataSet ();
   dad.Fill (ds);
   DataView dv = new DataView (ds.Tables [0]);
  }
  / * Execute SQL statements and return results * /
  public void readerData ()
  {
   connDb ();
   comm = new SqlCommand (sql, conn);
   dr = comm.ExecuteReader ();
  }
  / * Execute SQL statement without returning results * /
  public void exeSql ()
  {
   connDb ();
   comm = new SqlCommand (sql, conn);
   comm.ExecuteNonQuery ();
  }
  / * Close link * /
  public void clear ()
  {
   conn.Close ();
  }
 }
}
Reply

Use magic Report

0

Threads

56

Posts

21.00

Credits

Newbie

Rank: 1

Credits
21.00

 China

Post time: 2020-2-11 22:45:02
| Show all posts
Same as the web

string connStr = "....";
SqlConnection conn = new SqlConnection (connStr);
string sqlStr = "...";
SqlCommand cmd = new SqlCommand (sqlStr, conn);
SqlDataAdapter da = new SqlDataAdapter (cmd);
.....
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-23 09:30:01
| Show all posts
The second floor is just the sql server.

If it is Access
using System.Data.OleDb;
 public static OleDbConnection createOleconn () // Connection Access
  {
     string strConnection = "Provider = Microsoft.Jet.OleDb.4.0;";
     strConnection + = @ "Data Source = E:\net\datagridTest\CP2SysTempDB.mdb";
    // strConnection + = "Data Source =";
    // strConnection + = Server.MapPath ("CP2SysTempDB.mdb");


     OleDbConnection olconn = new OleDbConnection (strConnection);
     return olconn;
  }
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-2-24 15:30:01
| Show all posts
First ask what database ..
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 China

Post time: 2020-2-29 23:45:02
| Show all posts
Write an example of binding DataGridView:

using System.Data;
using System.Data.SqlClient;

SqlConnection con = new SqlConnection ("server = .; database = student; uid = sa; pwd = 0421");
SqlDataAdapter sda = new SqlDataAdapter ("select * from studentDetails", con);
DataSet ds = new DataSet ();
sda.Fill (ds, "student");
this.DataGridView.DataSource = ds.Tables ["student"];
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-2 08:45:01
| Show all posts
Access, I want to use DataGrid binding! How to do it
Reply

Use magic Report

0

Threads

110

Posts

63.00

Credits

Newbie

Rank: 1

Credits
63.00

 Ireland

Post time: 2020-3-2 18:30:01
| Show all posts
using System.Data.OleDb;

OleDbConnection con = new OleDbConnection ("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = d:\student.mdb;");
OleDbDataAdapter sda = new OleDbDataAdapter ("select * from studentDetails", con);
DataSet ds = new DataSet ();
sda.Fill (ds, "student");
this.DataGrid1.DataSource = ds.Tables ["student"];

// If it is WebForm, you have to add this sentence
//this.DataGrid1.DataBind ();
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-5 10:00:01
| Show all posts
Thank you! The problem has been solved!
Reply

Use magic Report

2

Threads

29

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-3-10 10:15:01
| Show all posts
The easiest way, drag a connection control up, there is a wizard
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list