在C#(azure数据库)中的gridview中添加一行
我有一个带有gridview的网页,它显示我的Azure cloud DB中某个表中的所有行。此表称为“students”,它包含id(主键)、名称和gpa 我还有两个文本框,一个用于名称,一个用于gpa。用户应该在这些框中键入一些值,然后单击“添加”按钮将新记录添加到表中 问题是我不能让它工作 以下是我在功能按钮中的内容,单击:在C#(azure数据库)中的gridview中添加一行,c#,azure,azure-sql-database,C#,Azure,Azure Sql Database,我有一个带有gridview的网页,它显示我的Azure cloud DB中某个表中的所有行。此表称为“students”,它包含id(主键)、名称和gpa 我还有两个文本框,一个用于名称,一个用于gpa。用户应该在这些框中键入一些值,然后单击“添加”按钮将新记录添加到表中 问题是我不能让它工作 以下是我在功能按钮中的内容,单击: string InsertCommand = @"INSERT INTO [Students] ([Student_Id], [Student_Name],
string InsertCommand = @"INSERT INTO [Students] ([Student_Id], [Student_Name],
[GPA]) VALUES (@Student_Id, @Student_Name, @GPA)";
string connString = "<%$ ConnectionStrings:SQLAzureConnection %>";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandText = InsertCommand;
comm.Parameters.AddWithValue("@Student_Name", TextBox1.Text);
comm.Parameters.AddWithValue("@GPA", TextBox2.Text);
try
{
conn.Open();
comm.ExecuteNonQuery();
}
string InsertCommand=@“插入到[学生]([学生Id],[学生姓名],
[GPA])值(@Student_Id、@Student_Name、@GPA)”;
字符串connString=“”;
使用(SqlConnection conn=newsqlconnection(connString))
{
使用(SqlCommand comm=newsqlcommand())
{
通信连接=连接;
comm.CommandText=插入命令;
comm.Parameters.AddWithValue(“@Student_Name”,TextBox1.Text);
comm.Parameters.AddWithValue(“@GPA”,TextBox2.Text);
尝试
{
conn.Open();
comm.ExecuteNonQuery();
}
**编辑:很抱歉,连接字符串不正确!!
我现在得到的是页面再次加载,但gridview保持不变
这是什么意思?我如何才能正确操作???看看您的代码: 您正在添加两个参数,但SQL似乎需要三个参数 作为参数输入的值缺少@Student\u Id 我的猜测也是,您的try-catch是空的,因此您永远看不到错误
James请发布您页面中的内容。\u加载我猜您没有处理Page.IsPostBack事件,只是重新加载了。@JamesKn是的,您是对的。因为我的页面\u加载目前是空的。应该有什么?