c# - SQL Server 插入

标签 c# asp.net .net sql sql-server

我有一个简单的两字段表单,将其数据存储在数据库中。由于某种原因,它不起作用。我已验证连接字符串有效,因为它在我制作的另一个项目中使用。

我没有包括第一个类的开头或其页面加载。

代码:

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string Name = txtName.Text;
        string Description = txtSpecial.Text;
        string method = string.Format(
            "INSERT INTO RbSpecials (Name,Description,Active) VALUES ('{0}','{1}','1')",
            Name,
            Description);
        RbConfiguration mySql = new RbConfiguration();
        try
        {
            mySql.Sql_Connection(method);
        }
        catch
        {

        }
    }
}

public class RbConfiguration
{
    string DbConnectionString = "System.Configuration.ConfigurationManager.ConnectionStrings['RBConnectionString'].ConnectionString";

    public void Sql_Connection(string queryString)
    {
        SqlConnection conn = new SqlConnection(DbConnectionString);
        SqlCommand cmd = new SqlCommand(queryString, conn);
        conn.Open();

        conn.Close();
    }
}

最佳答案

你永远不会execute你的 SQL 命令:

conn.Open(); 
cmd.ExecuteNonQuery(); 
conn.Close(); 

而且你的连接字符串是错误的(去掉双引号):

string DbConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["RBConnectionString"].ConnectionString;

关于c# - SQL Server 插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3604862/

相关文章:

c# - GridView 编辑/更新事件在 1 次单击后未触发

c# - 在单元测试中配置 log4net 以登录到控制台并显示正确的日期和时间

c# - 如何将 DataGridView 绑定(bind)到 SQLite 数据库?

c# - 从字符串请求中获取特定字符

javascript - ASPX - 如何从 Txt 文件读取行然后将文本存储在字符串中

c# - 当我在表单 onsubmit 中有函数时,Ajax 不起作用

c# - .NET Core 2 中间件解决方案中的 ClaimsIdentity 问题

c# - 如何获取WinForm上所有控件的列表,包括SplitContainers或Panels中的控件

c# - 当可见性改变时,mdi 子窗体绘制缓慢

asp.net - 如何将简单的 Dotnet 应用程序发布到 Ubuntu?