一个C#中的问题,提示当前上下文中不存在名称“conn“,这个conn是我建立的一个对象。代码如下。
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
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;
namespace Eg3_4
{
public partial class FrmConnState : Form
{
public FrmConnState()
{
SqlConnection conn= new SqlConnection("serve=(local);database=NoteTaking;Integrated Security=sspi");
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
conn.Open();
txtConnState.Text = conn.State.ToString();
btnOpen.Enabled = false;
btnClose.Enabled = true;
}
private void btnClose_Click(object sender, EventArgs e)
{
conn.Close();
txtConnState.Text = conn.State.ToString();
btnOpen.Enabled = true;
btnClose.Enabled = false;
}
}
} 展开
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;
namespace Eg3_4
{
public partial class FrmConnState : Form
{
public FrmConnState()
{
SqlConnection conn= new SqlConnection("serve=(local);database=NoteTaking;Integrated Security=sspi");
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
conn.Open();
txtConnState.Text = conn.State.ToString();
btnOpen.Enabled = false;
btnClose.Enabled = true;
}
private void btnClose_Click(object sender, EventArgs e)
{
conn.Close();
txtConnState.Text = conn.State.ToString();
btnOpen.Enabled = true;
btnClose.Enabled = false;
}
}
} 展开
1个回答
展开全部
你的conn得定义为字段
你定义在方法里面,
当然不能全局用了
public FrmConnState()
{
SqlConnection conn= new SqlConnection("serve=(local);database=NoteTaking;Integrated Security=sspi");
InitializeComponent();
}
把这个改为
SqlConnection conn= new SqlConnection("serve=(local);database=NoteTaking;Integrated Security=sspi");
public FrmConnState() {
InitializeComponent();
}
你定义在方法里面,
当然不能全局用了
public FrmConnState()
{
SqlConnection conn= new SqlConnection("serve=(local);database=NoteTaking;Integrated Security=sspi");
InitializeComponent();
}
把这个改为
SqlConnection conn= new SqlConnection("serve=(local);database=NoteTaking;Integrated Security=sspi");
public FrmConnState() {
InitializeComponent();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询