gpt4 book ai didi

c# - ASP.NET Web 应用程序 - 自从从 mysql 切换到 access db 以来,我的数据库连接出现问题

转载 作者:行者123 更新时间:2023-11-29 19:18:49 25 4
gpt4 key购买 nike

我为一个大学项目创建了一个 ASP.NET Web 应用程序,并且使用本地 mysql 数据库使其全部按预期工作,并具有完整功能。由于我无法控制的原因,我被指示将数据库更改为 Access 数据库。所以我已经这样做了,将“sql”语句更改为“Oledb”语句,我的应用程序的一半可以很好地适应新的更改,但我的另一半遇到问题。我将从 web.config 页面添加连接字符串,然后添加我遇到的问题。有人能看出我哪里出错了吗?

  <add name="newregDBConnectionString"  connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Alex\Documents\Database1.accdb"
providerName="System.Data.OleDb" />

我有两个问题,第一是我有一个注册页面,您填写一张表格,详细信息存储在数据库中。当我单击“提交”将详细信息发送到数据库时,出现错误;

RegIssue

第二个问题是我有一个gridview,它使用数据源显示注册的人员,但是当我单击查看按钮时,出现以下错误;

ViewIssue

我添加

"throw new Exception(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString);" to my global.aspx page to see the error and it showed the following;

Exception

这使得我的 web.config 页面中的连接字符串看起来不正确,但我的应用程序的另一半正在使用数据库连接(我的登录页面),所以我看不到问题。对这么长的帖子表示歉意,我非常感谢有人指出我愚蠢的地方!提前致谢。

更新:

protected void submitBtn_Click(object sender, EventArgs e)
{
OleDbConnection connect = new OleDbConnection(ConfigurationManager.ConnectionStrings["newregDBConnectionString"].ConnectionString);
{
if (parentRadBtn.Checked)
{
if (firstNameBox.Text == "" || surnameBox.Text == "" || postcodeBox.Text == "" || teleBox.Text == "" || emailBox.Text == "" || userBox.Text == "" || passwordBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
successLabel.Text = ("");
userBox.Text = "";
firstNameBox.Text = "";
surnameBox.Text = "";
postcodeBox.Text = "";
teleBox.Text = "";
emailBox.Text = "";
passwordBox.Text = "";
}
else
{
OleDbCommand pa = new OleDbCommand("INSERT INTO parent(parentID, firstname, surname, postcode, telephone, email, password) VALUES (@parentID, @firstname, @surname, @postcode, @telephone, @email, @password)", connect);
pa.Parameters.AddWithValue("@parentID", userBox.Text);
pa.Parameters.AddWithValue("@firstname", firstNameBox.Text);
pa.Parameters.AddWithValue("@surname", surnameBox.Text);
pa.Parameters.AddWithValue("@postcode", postcodeBox.Text);
pa.Parameters.AddWithValue("@telephone", teleBox.Text);
pa.Parameters.AddWithValue("@email", emailBox.Text);
pa.Parameters.AddWithValue("@password", passwordBox.Text);

connect.Open();
pa.ExecuteNonQuery();
connect.Close();
}

if (IsPostBack)
{
userBox.Text = "";
firstNameBox.Text = "";
surnameBox.Text = "";
postcodeBox.Text = "";
teleBox.Text = "";
emailBox.Text = "";
passwordBox.Text = "";
}

}
else if (childRadBtn.Checked)
{
if (firstNameBox.Text == "" || dayDobList.Text == "" || monthDobList.Text == "" || yearDobList.Text == "" || genderList.Text == "" || userBox.Text == "" || passwordBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry');</script>");
successLabel.Text = ("");
userBox.Text = "";
firstNameBox.Text = "";
dayDobList.Text = "";
monthDobList.Text = "";
yearDobList.Text = "";
genderList.Text = "";
passwordBox.Text = "";
}
else
{
OleDbParameter dob = new OleDbParameter("@dob", System.Data.SqlDbType.DateTime);
dob.Value = new DateTime(Int32.Parse(yearDobList.Text), Int32.Parse(monthDobList.Text), Int32.Parse(dayDobList.Text));

OleDbCommand ca = new OleDbCommand("INSERT INTO children(childID, firstname, dob, gender, password) VALUES (@childID, @firstname, @dob, @gender, @password)", connect);
ca.Parameters.AddWithValue("@childID", userBox.Text);
ca.Parameters.AddWithValue("@firstname", firstNameBox.Text);
ca.Parameters.Add(dob);
ca.Parameters.AddWithValue("@gender", genderList.Text);
ca.Parameters.AddWithValue("@password", passwordBox.Text);

connect.Open();
ca.ExecuteNonQuery();
connect.Close();
}
if (IsPostBack)
{
userBox.Text = "";
firstNameBox.Text = "";
dayDobList.Text = "";
monthDobList.Text = "";
yearDobList.Text = "";
genderList.Text = "";
passwordBox.Text = "";
}
}
}
}

最佳答案

看看这个:

OleDbConnection connect = new OleDbConnection("newregDBConnectionString");

注意到 newregDBConnectionString 是用引号括起来的吗?看起来这是保存连接字符串的变量的名称。通过将其放在引号中,您将传递文本“newregDBConnectionString”作为连接字符串本身,而不是该变量内的任何字符串。

您需要从配置管理器获取此连接字符串值,就像在第二个和第三个图像中所做的那样。

此外,在第二张图片中,您可以看到:

SqlDataSource source = new SqlDataSource();

需要将其更改为 OleDbDataSource

关于c# - ASP.NET Web 应用程序 - 自从从 mysql 切换到 access db 以来,我的数据库连接出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42547001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com