未处理SqlException

MILKZHOU 2009-05-31 05:57:56
文件一:主要是进行数据库的链接
class Mymeans
{
public static SqlConnection My_con;
public static string Login_ID = "";
public static string Login_Name = "";
public static string M_str_sqlcon = "Data Source=.;Database=db_PWMS;User id=;Pwd= ";
public static int Login_n = 0;
public static string AllSql = "Select * From 职工基本信息";


public static SqlConnection getcon()
{
My_con = new SqlConnection(M_str_sqlcon);
My_con.Open();
return My_con;
}

public void con_close()
{
if (My_con.State == ConnectionState.Open)
{
My_con.Close();
My_con.Dispose();
}
}

public SqlDataReader getcom(string SQLstr)
{
getcon();
SqlCommand My_com = My_con.CreateCommand();
My_com.CommandText = SQLstr;
SqlDataReader My_read = My_com.ExecuteReader();
return My_read;
}

public void getsqlcom(string SQLstr)
{
getcon();
SqlCommand SQLcom = new SqlCommand(SQLstr,My_con);
SQLcom.ExecuteNonQuery();
SQLcom.Dispose();
con_close();
}

public DataSet getDataSet(string SQLstr, string tableName)
{
getcon();
SqlDataAdapter SQLda = new SqlDataAdapter(SQLstr, My_con);
DataSet My_DataSet = new DataSet();
SQLda.Fill(My_DataSet, tableName);
con_close();
return My_DataSet;
}
}


文件二:一些公共类的设计
class MyModule
{
PMS.Mymeans MyDataClass =new PMS.Mymeans();
public static string ADDs="";
public static string FindValue = "";
public static string Address_ID="";
public static string User_ID ="";
public static string User_Name="";

public void Show_Form(string FrmName, int n)
{
if (n == 1)
{
if (FrmName == "人事档案管理")
{
PMS.F_MainFile FrmManFile = new F_MainFile();
FrmManFile.Text = "人事档案管理";
FrmManFile.ShowDialog();
FrmManFile.Dispose();
}

}
}

public void Clear_Control(Control.ControlCollection Con)
{
foreach (Control C in Con)
{
if (C.GetType().Name == "TextBox")
if (((TextBox)C).Visible == true)
((TextBox)C).Clear();
if (C.GetType().Name == "MaskedTextBox")
if (((MaskedTextBox)C).Visible == true)
((MaskedTextBox)C).Clear();
if (C.GetType().Name == "ComboBox")
if (((ComboBox)C).Visible == true)
((ComboBox)C).Text="";
if (C.GetType().Name == "PictureBox")
if (((PictureBox)C).Visible == true)
((PictureBox)C).Text="";

}
}

public string GetAutocoding(string TableName, string ID)
{
SqlDataReader MyDR = MyDataClass.getcom("select max(" + ID + ")NID from " + TableName);
int Num = 0;
if (MyDR.HasRows)
{
MyDR.Read();
if (MyDR[0].ToString() == "")
return "0001";
Num = Convert.ToInt32(MyDR[0].ToString());
++Num;
string s = string.Format("{0:0000}", Num);
return s;
}
else
{
return "0001";
}
}

public void Ena_Button(Button B1, Button B2, Button B3, Button B4, int n1, int n2, int n3, int n4)
{
B1.Enabled = Convert.ToBoolean(n1);
B2.Enabled = Convert.ToBoolean(n2);
B3.Enabled = Convert.ToBoolean(n3);
B4.Enabled = Convert.ToBoolean(n4);
}
}


文件三:在窗口中单击“添加”按钮时,则首先会调用Clear_Control方法,将指定控件集下的控件进行清空,然后根据表名和ID字段调用GetAutocoding方法进行自动编号。

public partial class F_MainFile : Form
{
public F_MainFile()
{
InitializeComponent();
}

PMS.Mymeans MyDataClass = new PMS.Mymeans();
PMS.MyModule MyMC = new PMS.MyModule();
public static DataSet MyDS_Grid;
public static string tem_Field = "";
public static string tem_Value = "";
public static string tem_ID = "";
public static int hold_n = 0;
public static byte[] imgBytesIn; //用来存储图片的二进制数
public static int Ima_n = 0; //判断是否对图片进行了操作
public static string Part_ID = ""; //存储数据表的ID信息

private void Sut_Add_Click(object sender, EventArgs e)
{
MyMC.Clear_Control(tabControl1.TabPages[0].Controls);
S_0.Text = MyMC.GetAutocoding("职工基本信息", "职工编号");
hold_n = 1;
MyMC.Ena_Button(Sut_Add, Sut_Amend, Sut_Cancel, Sut_Save, 0, 0, 1, 1);
groupBox5.Text = "当前正在添加信息";
Img_Clear.Enabled = true;
Img_Save.Enabled = true;

}
}


文件四:数据库内容
数据库名为db_PWMS
其中的一个表,名为:职工基本信息
此表内容为:
列名 数据类型
职工编号 varchar(5)
职工姓名 varchar(20)


进行调试
当单击“添加”按钮时
会出现以下错误:
未处理SqlException
‘ ‘附件有语法错误

此时错误的箭头会指向以下数据库链接语句的黄色处
public SqlDataReader getcom(string SQLstr)
{
getcon();
SqlCommand My_com = My_con.CreateCommand();
My_com.CommandText = SQLstr;
SqlDataReader My_read = My_com.ExecuteReader();
return My_read;
}

如何去解决这个问题呢?
...全文
620 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
静之 2012-02-22
  • 打赏
  • 举报
回复
如果还是运行不了怎么办?我也出现这样的问题,不是空格问题
MILKZHOU 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yechangda 的回复:]
SqlDataReader MyDR = MyDataClass.getcom("select max(" + ID + ")NID from " + TableName);
NID和(之间缺少空格
[/Quote]

确实是因为空格的问题而做不了
MILKZHOU 2009-06-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yechangda 的回复:]
SqlDataReader MyDR = MyDataClass.getcom("select max(" + ID + ")NID from " + TableName);
NID和(之间缺少空格
[/Quote]

NID是不是指求哪一列数的最大的一个啊?
yechd 2009-05-31
  • 打赏
  • 举报
回复
SqlDataReader MyDR = MyDataClass.getcom("select max(" + ID + ")NID from " + TableName);
NID和(之间缺少空格
os340223 2009-05-31
  • 打赏
  • 举报
回复
要么连接字符串有问题。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧