为什么我不能在数据库中存储值?我的代码是正确的。当我访问数据库时,没有显示任何值。但是当我执行时,它的实现是正确的。告诉我该怎么做才能使它正确。
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();
}
}
}
答案 0 :(得分:1)
就我们所知,您还没有正确连接button1_submit_Click
事件!
我建议您使用一些简单的测试数据执行button1_submit_Click
中的代码,看看您的数据库/权限是否存在问题....(缺少存储过程,参数不正确)..或者是否存在您的用户界面存在问题
只需在没有来自用户界面的任何互动的情况下运行它,这应该可以帮助解决任何问题。
还尝试在Management Studio中运行存储过程,看看会发生什么。
您是否考虑过using而不是您正在做的事情。
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();
}