Today is March 10, there are 5 days before the second round of the Chinese laborers. The second-round test is the database, with VS and SQL Server to do the problem. It's not very difficult. Here are the notes I'm going to try.
First, the database design
- Build Index
Create INDEX <index-name> on <relation-name> (<attribute-list>)
Example:CREATE index Dept-index on instructor (dept_name)
- Create a View
Create View V as <query-expression>
Example:CREATE view faulty as
Select Id,name,dept_name
From instructor
- Default value
Default 0
- Check constraint
Check (P)//p as Condition
Example Check (size<10)// allow only records with size less than ten to be inserted
- cascading deletes and cascading updates
Foreign KEY (dept_name) References Department
ON DELETE Cascade
ON UPDATE cascade
- Fuzzy search
SELECT field from table WHERE a field like condition
Conditions :
①% : Represents any of 0 or more characters.
②_ : Represents any single character.
③[] : Represents one of the characters listed in parentheses (similar to a regular expression).
④[^] : Represents a single character that is not listed in parentheses.
⑤ query content contains wildcard characters: use [] to enclose a special character .
Second, the database programming
- Connecting database statements
server=<ip>;D atabase=< database name >;integrated security=false; uid=< Login name >; pwd=< password >
server= (local);D atabase=< database name >;integrated Security=sspi
- Insert, UPDATE, delete method
public static void Executenoquery (String sql)
{
Try
{
SqlCommand cmd=new Sqlcommad (sql,sqlutil.conn);
if (cmd. Excutenoquery () >0)
{
MessageBox.Show ("successful operation");
}
Else
{
MessageBox.Show ("Operation failed");
}
}
catch (Exception ex)
{
MessageBox.Show (ex. Message);
}
}
- Method of Select
public static DataTable ExecuteQuery (String sql)
{
Try
{
DataTable table = new DataTable ();
DataAdapter adapter = new DataAdapter (sql,sqlutil.conn);
Adapter. Fill (table);
return table;
}
catch (Exception ex)
{
MessageBox.Show (ex. Message);
return null;
}
}
- Connect Database methods
Try
{
SqlConnection conn = new SqlConnection (SQLUTIL.CONNSTR);
Conn. Open ();
}
catch (Exception ex)
{
MessageBox.Show (ex. Message);
Applitcation.exit ();
Return
}
- To close a database method
SqlUtil.conn.Close (); no Dispose (), because the connection resource is recycled
- DataGridView Binding Data Method
Datagridview1.datasource = table;
- Drop-down box Change event
SelectedIndexChanged
- Get DataGridView The first element of the first row in a selected row
This.datagridview1.selectedrows[0]. Cell[0]. Value;
- Delete DataGridView selected first row
DataGridView1.Rows.RemoveAt (Datagridview1.selectedrows[0].index);
- Set DataGridView to select only rows ( not cells )
Datagridview1.selectionmode=datagridviewselcitonmode.fullrowselect;
- Set DataGridView read-only
Datagridview1.readonly = true;
- YesNo Confirmation Box
DialogResult dr = MessageBox.Show (this, "OK delete ?", "hint", messageboxbutton.yesno);
if (dr==dialogresult.yes)// Click OK after the code
- Form Reactivation Event ( to refresh the table after the Add and modify windows are closed )
Activated
- Refresh Table Window Code
private void Refresh ()
{
String Sql= "select * FROM < table name >";
DataTable table=sqlutil.executequery (SQL);
Datagridview1.datasource = table;
}
Chinese laborers ' Second interview study notes