添加数据库包:
数据库连接时 发生错误:
解决方式:
SqlConnection conn = new SqlConnection("server=localhost;database=MyBBSDb;uid=sa;pwd=123456;Encrypt=false;") ;conn.Open();SqlCommand cmd = new SqlCommand("SELECT * FROM Users",conn);SqlDataAdapter sda=new SqlDataAdapter (cmd);DataSet ds = new DataSet();sda.Fill(ds);DataTable res = ds.Tables[0];DataRow dr = res.Rows[0];var value = dr["UserNo"].ToString();
关闭数据库的方法一:关闭数据库的方法二:
关闭数据的方法三:
数据的插入:
[HttpPost]public string Insert(string userNo,string userName,int userLevel,string password){using SqlConnection conn = new SqlConnection("server=.;database=MyBBSDb;uid=sa;pwd=123456;Encrypt=false;");conn.Open();SqlCommand cmd = new SqlCommand($"INSERT INTO Users(UserNo,UserName,UserLevel,PassWord) VALUES('{userNo}','{userName}',{userLevel},'{password}')", conn);cmd.ExecuteNonQuery();return "全栈ACE";}