Basic steps for manipulating a database are Shared in ASP.NET

  • 2020-05-16 06:38:24
  • OfStack

1. Basic steps of ASP.NET operation database:

Common methods for data manipulation:
a. ExecuteReader()
One SqlDataReader object or one OleDbDataReader object is returned, and one record is saved in the memory of the server at a time.
Specific fast access capabilities compared to DataSet, typically used for query operations.
b.ExecuteNonQuery()
c.ExecuteScalar () returns the Object type. If SELECT is executed, the result is row 1 and column 1 after the query
Returns the number of rows affected in the database, the first choice for database transactions.
public int test()
{
string connStr = @"server=Miro; database=newssystem; uid=sa; pwd=sa";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string sql = "insert into category(name) values('ttt')";
SqlCommand cmd = new SqlCommand(sql, conn);
int res=cmd.ExecuteNonQuery();
conn.Close();
return res;
}
public DataTable test (string sql)
{
DataTable dt = new DataTable ();
string connStr = @"server=Miro; database=newssystem; uid=sa; pwd=sa";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataReader sdr = cmd. ExecuteReader ();
dt.Load(sdr);
sdr. Close ();
conn.Close();
return dt;
}
2. VS tips:
You can add startup without debugging to the toolbar: tools -- > The custom - > The command - > debugging

Related articles: