Essay:
Add Tb_user table data to DataGridView
stringSqlconn ="Data source=localhost;initial catalog=db_qq; Persist Security info=true; User Id=sa; password=99990000"; SqlConnection Conn=NewSqlConnection (sqlconn); Conn. Open (); SqlCommand cmd=Conn. CreateCommand (); Cmd.commandtype=CommandType.Text; Cmd.commandtext="SELECT * from Tb_user"; SqlDataAdapter SDA=NewSqlDataAdapter (CMD); DataSet DS=NewDataSet ();
"User" can be changed to any string (-empty string) SDA. Fill (DS,the user"); Conn. Close (); Datagridview1.datasource= ds. tables[the user"];
Insert data into SQL Server (placeholders and non-use placeholders):
Do not use placeholders
Cmd.commandtext ="INSERT into Tb_user (IP, Port, Name, Password, State) VALUES (1, 1, 1, 1, 1)"; Cmd. ExecuteNonQuery ();
Use placeholder Cmd.commandtext="INSERT into Tb_user (IP, Port, Name, Password, State) VALUES (@IP, @Port, @Name, @Password, @State)"; Cmd. Parameters.addwithvalue ("@IP","2"); Cmd. Parameters.addwithvalue ("@Port","2"); Cmd. Parameters.addwithvalue ("@Name","2"); Cmd. Parameters.addwithvalue ("@Password","2"); Cmd. Parameters.addwithvalue ("@State","2"); Cmd. ExecuteNonQuery ();
C # connecting SQL Server