C#调用类出现的问题错误 1 类、结构或接口成员声明中的标记“=”无效
form1里:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSy...
form1里:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnShow_Click(object sender, EventArgs e)
{
string cmdText = "select * from manageMent";
SqlDataAdapter da = new SqlDataAdapter(cmdText,sqlCon);
DataTable dtManman = new DataTable();
try
{
sqlCon.Open();
da.Fill(dtManman);
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally { sqlCon.Close(); }
dgvMan.DataSource = dtManman;
}
}
}
新建类Class1里:
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication1
{
class Class1
{
string connectionstring = "Data Source=.;Database=pubs;Uid=sa;Pwd=sa";
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = connectionstring;
}
}
然后出现 ,求解决,调用类方面。 展开
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnShow_Click(object sender, EventArgs e)
{
string cmdText = "select * from manageMent";
SqlDataAdapter da = new SqlDataAdapter(cmdText,sqlCon);
DataTable dtManman = new DataTable();
try
{
sqlCon.Open();
da.Fill(dtManman);
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
finally { sqlCon.Close(); }
dgvMan.DataSource = dtManman;
}
}
}
新建类Class1里:
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsApplication1
{
class Class1
{
string connectionstring = "Data Source=.;Database=pubs;Uid=sa;Pwd=sa";
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = connectionstring;
}
}
然后出现 ,求解决,调用类方面。 展开
3个回答
展开全部
把sqlCon.ConnectionString = connectionstring;这句放到构造函数里
class Class1
{
string connectionstring = "Data Source=.;Database=pubs;Uid=sa;Pwd=sa";
SqlConnection sqlCon = new SqlConnection();
public Class1()
{
sqlCon.ConnectionString = connectionstring;
}
}
展开全部
倒数第3行语法错误,要么构造初始化,要么使用时给连接串。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sqlCon.ConnectionString = connectionstring;这句赋值的语句,必须写在方法中,不能和定义一样直接写在类下面。
private void a()
{
sqlCon.ConnectionString = connectionstring;
}
private void a()
{
sqlCon.ConnectionString = connectionstring;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询