随笔: // 将tb_User表数据添加到DataGridView中 string sqlconn = "Data Source=localhost;Initial Catalog=db_QQ;Persist Security Info=True;User ID=sa;Password=99990000"; SqlConnection conn = new SqlConnection(sqlconn); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT * FROM tb_User"; SqlDataAdapter sda = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); // "user" 可改为任意字符串(-空串) sda.Fill(ds, "user"); conn.Close(); dataGridView1.DataSource = ds.Tables["user"];
向SQLServer插入数据(占位符和不使用占位符):
// 不使用占位符 cmd.CommandText = "INSERT INTO tb_User(IP, Port, Name, Password, State) VALUES(1, 1, 1, 1, 1)"; cmd.ExecuteNonQuery(); // 使用占位符 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();
时间: 2024-11-11 22:07:47