我在 ASP.NET Core MVC 中创建了项目,因为我在单个项目中有两个独立的功能意味着框架,一个是 .NET 类,第二个是 ASP.NET Core 8 Web 应用程序,该 Web 应用程序引用了 .NET 类。
在.NET类中,我创建了ADO.NET文件,其中存储所有数据库属性。在网络应用程序中,存储了所有网站相关的内容和代码。
现在我正在尝试借助Azure中的Azure SQL数据库来部署这个项目。它成功并创建了数据库并进行了部署,但是在本地 SQL Server 上创建的表没有被复制 - 这意味着它不会显示在 Azure SQL 云数据库中;仅创建数据库,而不创建表。
任何人都可以建议做什么或任何教程吗?
以下步骤可在 Azure SQL Server 中使用 ASP .NET Core MVC 和 ADO .NET 创建用于管理学生记录的 Web 应用程序,并将其部署在 Azure 上:
public static class ConnectionString
{
// Azure SQL Server details
private static string cName = "<your_server_name>.database.windows.net,1433;Initial Catalog=<your_database_name>;Persist Security Info=False;User ID=<your_user_id>;Password=<your_password>;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30";
public static string CName { get => cName; }
}
public class Student
{
public int Id { set; get; }
[Required]
public string FirstName { set; get; }
[Required]
public string LastName { set; get; }
[Required]
public string Email { set; get; }
[Required]
public string Mobile { set; get; }
public string Address { set; get; }
}
public class StudentController : Controller
{
// GET: Student
public ActionResult Index()
{
return View();
}
// GET: Student/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Student/Create
public ActionResult Create()
{
return View();
}
// POST: Student/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
// GET: Student/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: Student/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, IFormCollection collection)
{
try
{
// TODO: Add update logic here
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
// GET: Student/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Student/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
}
public class StudentDataAccessLayer
{
string connectionString = ConnectionString.CName;
public IEnumerable<Student> GetAllStudent()
{
List<Student> lstStudent = new List<Student>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spGetAllStudent", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Student student = new Student();
student.Id = Convert.ToInt32(rdr["Id"]);
student.FirstName = rdr["FirstName"].ToString();
student.LastName = rdr["LastName"].ToString();
student.Email = rdr["Email"].ToString();
student.Mobile = rdr["Mobile"].ToString();
student.Address = rdr["Address"].ToString();
lstStudent.Add(student);
}
con.Close();
}
return lstStudent;
}
public void AddStudent(Student student)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spAddStudent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@FirstName", student.FirstName);
cmd.Parameters.AddWithValue("@LastName", student.LastName);
cmd.Parameters.AddWithValue("@Email", student.Email);
cmd.Parameters.AddWithValue("@Mobile", student.Mobile);
cmd.Parameters.AddWithValue("@Address", student.Address);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
public void UpdateStudent(Student student)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spUpdateStudent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", student.Id);
cmd.Parameters.AddWithValue("@FirstName", student.FirstName);
cmd.Parameters.AddWithValue("@LastName", student.LastName);
cmd.Parameters.AddWithValue("@Email", student.Email);
cmd.Parameters.AddWithValue("@Mobile", student.Mobile);
cmd.Parameters.AddWithValue("@Address", student.Address);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
public Student GetStudentData(int? id)
{
Student student = new Student();
using (SqlConnection con = new SqlConnection(connectionString))
{
string sqlQuery = "SELECT * FROM Student WHERE Id= " + id;
SqlCommand cmd = new SqlCommand(sqlQuery, con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
student.Id = Convert.ToInt32(rdr["Id"]);
student.FirstName = rdr["FirstName"].ToString();
student.LastName = rdr["LastName"].ToString();
student.Email = rdr["Email"].ToString();
student.Mobile = rdr["Mobile"].ToString();
student.Address = rdr["Address"].ToString();
}
}
return student;
}
public void DeleteStudent(int? id)
{
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spDeleteStudent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Id", id);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
本地:
蔚蓝: