连接属性尚未初始化

连接属性尚未初始化

本文介绍了exExecuteNonQuery:连接属性尚未初始化.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.Configuration;
namespace air
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static string constring = ConfigurationManager.ConnectionStrings["Cnstr"].ConnectionString;
        static SqlConnection con = new SqlConnection(constring);

        private void button1_Click(object sender, EventArgs e)
        {

            SqlCommand cmd = new SqlCommand("in_mem");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@MemNo", txtmbm.Text);
            cmd.Parameters.AddWithValue("@name", txtnomname.Text);
            cmd.Parameters.AddWithValue("@deptno",txtdept.Text);
            cmd.Parameters.AddWithValue("@empno",txtemp.Text);
            cmd.Parameters.AddWithValue("@d_o_m", txtdom.Text);
            cmd.Parameters.AddWithValue("@d_o_b", txtdob.Text);
            cmd.Parameters.AddWithValue("@d_o_r", txtdor.Text);
            cmd.Parameters.AddWithValue("@nominee_name",txtnomname.Text);
            cmd.Parameters.AddWithValue("@relationship", txtrlt.Text);
            cmd.Parameters.AddWithValue("@address",txtaddr.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}
and my connection string

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings >
    <add name="cnstr" providerName="System.Data.SqlClient"
              connectionString="Data Source=localhost;Initial Catalog=airindia_db; Integrated Security=true" />
  </connectionStrings>
</configuration>

推荐答案


SqlCommand cmd = new SqlCommand("in_mem",con);


这篇关于exExecuteNonQuery:连接属性尚未初始化.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 11:12