c# - 为什么我的提交按钮没有通过它的 for 循环?
问题描述
我目前有一个插入按钮,允许三个人进入一个数组,一旦单击提交按钮,它应该通过插入按钮实例化的数组并将学生插入我的 SQL 数据库。目前它只插入第一个插入数组的学生。另外,我知道这没有任何形式的安全性,我正在学习课程并且还没有得到任何参数化查询或跨站点脚本。一旦我得到它会更新。
protected void Commit_Click(object sender, EventArgs e)
{
int a = 0;
a = (int)Session["count"];
SqlConnection sc = new SqlConnection
{
ConnectionString = @"Server =LocalHost;Database=Lab1;Trusted_Connection=Yes;"
};
sc.Open();
SqlCommand insert = new SqlCommand();
insert.Connection = sc;
for (int i = 0; i < a; i++)
{
DateTime Dob = Convert.ToDateTime(DOB.Text);
resultMessage.Text = "";
maxID++;
DateTime queryLastUpdated = Student.newStudents[i].LastUpdated;
queryLastUpdated.ToString("YYYY-MM-DD");
if (MiddleInitial.Text == "")
{
try
{
insert.CommandText = "INSERT INTO [dbo].[Student] ([FirstName],[LastName],[HouseNumber],[Street],[CityCounty],[HomeState],[Country],[Zip],[DateOfBirth]" +
",[LastUpdatedBy],[LastUpdated],[PropertyID]) VALUES ('" +
Student.newStudents[i].FirstName + "', '" + Student.newStudents[i].LastName
+ "', '" + Student.newStudents[i].HouseNumber
+ "', '" + Student.newStudents[i].Street + "', '" + Student.newStudents[i].CityCounty
+ "', '" + Student.newStudents[i].State + "', '" + Student.newStudents[i].Country
+ "', '" + Student.newStudents[i].ZipCode + "', '" + Convert.ToDateTime(DOB.Text)
+ "', '" + Student.newStudents[i].LastUpdatedBy + "', '" + queryLastUpdated
+ "', '" + Student.newStudents[i].PropertyID + "')";
insert.ExecuteNonQuery();
}
catch (Exception)
{
resultMessage.Text = "Error Clearing Database.";
}
}
else
{
try
{
insert.CommandText = "INSERT INTO [dbo].[Student] ([FirstName],[LastName],[MiddleName],[HouseNumber],[Street],[CityCounty],[HomeState],[Country],[Zip],[DateOfBirth]" +
",[LastUpdatedBy],[LastUpdated],[PropertyID]) VALUES ('"
+ Student.newStudents[i].FirstName + "', '" + Student.newStudents[i].LastName
+ "', '" + Student.newStudents[i].MiddleName + "', '" + Student.newStudents[i].HouseNumber
+ "', '" + Student.newStudents[i].Street + "', '" + Student.newStudents[i].CityCounty
+ "', '" + Student.newStudents[i].State + "', '" + Student.newStudents[i].Country
+ "', '" + Student.newStudents[i].ZipCode + "', '" + Convert.ToDateTime(DOB.Text)
+ "', '" + Student.newStudents[i].LastUpdatedBy + "', '" + queryLastUpdated
+ "', '" + Student.newStudents[i].PropertyID + "')";
insert.ExecuteNonQuery();
}
catch (Exception)
{
resultMessage.Text = "Error Clearing Database.";
}
}
Student.counter = 0;
Array.Clear(Student.newStudents, 0, Student.newStudents.Length);
Array.Clear(Student.studentCopy, 0, Student.studentCopy.Length);
}
sc.Close();
}
解决方案
我认为您的代码的问题在于您在循环的第一次迭代之后清除了数组:
Array.Clear(Student.newStudents, 0, Student.newStudents.Length);
这样,您的数组现在可以容纳大量空对象(您的类的默认值)(Array.Clear) - 并且没有必要从中插入任何有意义的东西。
除此之外还有几点建议:
使用“使用”,它会让你的生活更轻松:)
你真的不需要循环中的 if 条件 - 我无法发现 sql 命令的真正区别(但可能是错误的......)。开发的一般经验法则应该是“可读性和简单性统治世界”:)
如评论中所述 - 如果您直接使用数组 - 检查其长度而不是将其存储在会话中。
queryLastUpdated.ToString("YYYY-MM-DD");
什么都不为你做。您需要将字符串存储在某处,如果您想使用它,
最后,对于stackoverflow,最好发布最少量的所需代码,这将使您的问题变得更好!
推荐阅读
- java - 当 URL 包含特殊字符时,HttpsURLConnection 似乎会误导 openConnection()
- python - Python 词组标注
- python - 需要可选参数的困难 python curve_fit 多维任务
- python - 将嵌套的 Json 读入 Pandas 错误
- php - cPanel 数据库的配置文件导致 500(内部服务器错误)
- javascript - 如何使这个导航从右到左打开?
- c++ - operator= 重载和字符串值的返回类型
- python - 通过多个条件添加新列
- java - 反向字符串方法类型不匹配错误java
- ssl - 使用 TLS 保护 Kubernetes 服务