C# 当我的代码正确时,为什么不能在数据库中存储值?

C# 当我的代码正确时,为什么不能在数据库中存储值?,c#,C#,为什么我不能在数据库中存储值?我的代码是正确的。当我访问数据库时,没有显示任何值。但当我执行时,它的实现是正确的。告诉我怎么做才能使它正确 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.For

为什么我不能在数据库中存储值?我的代码是正确的。当我访问数据库时,没有显示任何值。但当我执行时,它的实现是正确的。告诉我怎么做才能使它正确

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Configuration;

namespace ODesk
{
    public partial class Form8registration : Form
    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);

        public Form8registration()
        {
            InitializeComponent();
        }

        private void linkLabel1_login_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form1 fm1 = new Form1();
            fm1.Show();
            this.Hide();

        }

        private void Form8registration_Load(object sender, EventArgs e)
        {
            conn.Open();                

        }

        private void button2_reset_Click(object sender, EventArgs e)
        {
            Reset();
        }

        private void Reset()
        {
            textBox1_add.Text = "";
            textBox3_phoneno.Text = "";
            textBox4_emailid.Text = "";
            textBox5_desgination.Text = "";
            textBox6_name.Text = "";
            textBox7_empid.Text = "";
            comboBox1_shifttime.SelectedText  = "";
        }

        private void button3_cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_submit_Click(object sender, EventArgs e)
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }              

                    SqlCommand cmdd = new SqlCommand("registrtn", conn);

                    cmdd.CommandType = CommandType.StoredProcedure;
                    cmdd.Connection = conn;

                    cmdd.Parameters.Add("@empid",SqlDbType.VarChar,50).Value = textBox7_empid.Text;
                    cmdd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = textBox6_name.Text;
                    cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 50).Value = textBox5_desgination.Text;
                    cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 50).Value = textBox4_emailid.Text;
                    cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 50).Value = textBox3_phoneno.Text;
                    cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 50).Value = textBox1_skpid.Text;
                    cmdd.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = textBox1_add.Text;
                    cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 50).Value = comboBox1_shifttime.SelectedText;
                    cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 50).Value = radioButton1_employe.Checked.ToString();


                    cmdd.ExecuteNonQuery();
                    cmdd.Dispose();
                    conn.Close();
                    Reset();
        }
    }
} 

据我们所知,您尚未正确连接按钮1\u提交\u单击事件

我建议您使用一些简单的测试数据执行button1\u submit\u中的代码,并查看您的数据库/权限是否存在问题…缺少存储过程,参数不正确。。或者您的UI是否存在问题

只需在不与UI进行任何交互的情况下运行它,这将有助于解决任何问题

还可以尝试在ManagementStudio中运行存储过程,看看会发生什么

你是否考虑过而不是你正在做什么

using(var conn = new SqlConnection(connString))
{
   SqlCommand cmdd = new SqlCommand("registrtn", conn);
   cmdd.CommandType = CommandType.StoredProcedure;
   cmdd.Parameters.Add("@empid",SqlDbType.VarChar,50).Value = textBox7_empid.Text;
   cmdd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = textBox6_name.Text;
   cmdd.Parameters.Add("@desgination", SqlDbType.VarChar, 50).Value = textBox5_desgination.Text;
   cmdd.Parameters.Add("@emailid", SqlDbType.VarChar, 50).Value = textBox4_emailid.Text;
   cmdd.Parameters.Add("@phone", SqlDbType.VarChar, 50).Value = textBox3_phoneno.Text;
   cmdd.Parameters.Add("@skype", SqlDbType.VarChar, 50).Value = textBox1_skpid.Text;
   cmdd.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = textBox1_add.Text;
   cmdd.Parameters.Add("@shifttime", SqlDbType.VarChar, 50).Value = comboBox1_shifttime.SelectedText;
   cmdd.Parameters.Add("@panel", SqlDbType.VarChar, 50).Value = radioButton1_employe.Checked.ToString();

   cmdd.ExecuteNonQuery();
}

您是否验证了您是以预期用户的身份附加的,存储过程是否存在,以及用户是否有权访问数据库和过程?显然,您的代码不正确。请将示例代码缩短到所需的长度。close方法的作用是什么?您的C代码看起来正常您不需要设置cmdd.Connection=conn,因为它已设置,但存储过程注册表中有什么?button1\u submit\u Click方法中是否遇到任何错误,表明连接字符串存在问题或存储过程不存在/用户无法使用?请删除不必要的代码,只显示最相关的代码。还显示SP registernits不工作的详细信息值不能存储在数据库中是的,我尝试过这就是为什么我在iu执行时对u、、、和存储过程说,然后它的实现正确,也存储在数据库中,,,但当我在字段中输入值后运行我的窗口窗体时,它不会显示在数据库中