C#操作数据库问题(mysql)

cainiao66 2008-12-21 10:02:15
if (bookNumber.Text.Trim().Length > 0 | text.Text.Trim().Length > 0)
{

try
{
//建立与mysql的连接
MySQLConnection DBConn = null;
DBConn = new MySQLConnection(new MySQLConnectionString("localhost", "mybase", "root", "tizheng", 3306).AsString);
DBConn.Open();
MySQLCommand conn = new MySQLCommand("set names gb2312", DBConn);
conn.ExecuteNonQuery();
string insstr = "insert into books(bookList,text,setPeople,time)";
insstr += "values('" + Convert.ToString(bookNumber.Text.Trim()) + "','" + Convert.ToChar(text.Text.Trim()) + "','" + Convert.ToString(ClassString.UserName) + "','" + Convert.ToDateTime(ClassString.time) + "')";
MySQLDataAdapter myAD = new MySQLDataAdapter(insstr, DBConn);
DataSet myDS = new DataSet();
myAD.Fill(myDS, "books");
DBConn.Close();
MessageBox.Show("账簿建立成功!");
}
catch (Exception oe) { MessageBox.Show(oe.Message, "数据库出错!"); }
}
else { MessageBox.Show("初始账簿数据不能为空!", "提示"); }
以下都为1为错误2为异常不知道如何处理:
1,添加数据时提示:字符串长度只为一个字符(数据库中所有字段都大于10);
2,出现数据库操作异常:值不能为空,参数名:dataReader(不知道怎么了,肯定的是参数都有值,而且数据库内应经插入数据)
3,但是日期不知道为什么是这种形式:21:29:58(我想弄日期模式结果成时间模式)
public static string UserName;//登陆用户账号
public static DateTime time = System.DateTime.Now;//复制系统时间
以上为静态类里的参数
不知道如何对时间方法行进重写了
如果哪位有C#操作数据库mysql的案例发下地址好的也给分
4,还有个C#中的muneStrip插件
如何进行更新问题:
例如
this.menuItemPower.Enabled = true;
this.insertCustermer.Enabled = false;
菜单为一个灰色不可用
如何update这些参数进行重绘
...全文
250 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cainiao66 2008-12-23
  • 打赏
  • 举报
回复
有人帮解决下四吗
不知道怎么弄。。。。。。。
谢谢
对月成双 2008-12-22
  • 打赏
  • 举报
回复
获得时间最简单的方法,你看看
string time=DateTime.Now.Tostring();
结果time显示的格式是 年-月-日 时-分-秒
如果你希望得到日期或者时间,再用个.substring()方法,就OK了
cainiao66 2008-12-22
  • 打赏
  • 举报
回复
问题1,2,3已经解决了
谁能帮我把问题四解决下
谢谢
bbbbbb888888 2008-12-22
  • 打赏
  • 举报
回复
,'" + Convert.ToDateTime(ClassString.time) + "')"
最好用mysql的内置varchar->datetime的函数.
cainiao66 2008-12-22
  • 打赏
  • 举报
回复

问题4:可以在需要的时候在代码中添加this.insertCustermer.Enabled = true;

[/Quote]
这个我知道
我的意思不是说灰色不知道怎么变可用
而是通过一个if判断分支
然后分别显示
但是想通过更新真值运行else分支
cainiao66 2008-12-22
  • 打赏
  • 举报
回复
第一,二,三
我发现是数据库的问题
我换了MS-sql就没有问题了
cunzhangok 2008-12-21
  • 打赏
  • 举报
回复
很明显,你的数据类型设置错误了,注意字符型还是text型,你把所有你数据库中的所有字符类型的换成text看看行不行
zlhzjg 2008-12-21
  • 打赏
  • 举报
回复
问题3:在程序中这么写System.DateTime.Now.ToString("yyyy-MM-dd");或者在控制面板中“区域和语言选项”,将中时间格式设定为
yyyy--MM-dd
问题4:可以在需要的时候在代码中添加this.insertCustermer.Enabled = true;
至于问题1和2,这里有一段的代码,你先借鉴一下:

string connection = System.Configuration.ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ToString();
private string m_strcon = System.Configuration.ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ToString();
private BindingSource m_bs1 = new BindingSource();
private DataSet m_ds = new DataSet();
private SqlConnection m_con = new SqlConnection();
private SqlDataAdapter m_da = new SqlDataAdapter();
private void btnsave_Click(object sender, EventArgs e)
{
int flag = 0;
m_con = new SqlConnection(m_strcon);
m_con.ConnectionString = connection;
m_con.Open();
SqlCommand com1;
com1 = new SqlCommand("select distinct empID from emp", m_con);
SqlDataReader dr1 = com1.ExecuteReader();
while(dr1.Read())
{
if (int.Parse(txtEmployeeID.Text) == dr1.GetInt32(0))
{
MessageBox.Show("Employee " + txtEmployeeID.Text + "has exist,please enter another person");
flag = 1;
}
}
if(flag==0)
{
if (cmbAllocProjID.Text == "" || txtEmployeeID.Text == "" || textBox2.Text == "" || comboBox2.Text == "")
{
MessageBox.Show("You cannot leave the text field blank", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlConnection con;
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ToString();
con = new SqlConnection(constring);
string str1 = null;
str1 = "insert into emp (empID,Name,ProjID,ResourceAlloc) values('" + txtEmployeeID.Text.ToString() + "','" + textBox2.Text.ToString() + "','" + cmbAllocProjID.SelectedItem.ToString() + "','" + comboBox2.SelectedItem.ToString() + "')";
SqlCommand com = new SqlCommand(str1, con);
com.CommandType = CommandType.Text;
con.Open();
com.ExecuteNonQuery();
con.Close();
txtEmployeeID.Text = "";
textBox2.Text = "";
comboBox2.Text = "";
cmbAllocProjID.Text = "";
SqlConnection m_con1;
DataSet m_ds1 = new DataSet();
string constring1 = System.Configuration.ConfigurationManager.ConnectionStrings["HRMSConnectionString"].ToString();
//instantiating the object of SqlConnection
m_con1 = new SqlConnection(constring1);
string strQuery = "Select * from emp";
m_da = new SqlDataAdapter(strQuery, m_con1);
//Fill method to fill the DataSet object
m_da.Fill(m_ds1, "emp");
//m_bs1.DataSource = m_ds.Tables[0];
objdataGridView.AutoGenerateColumns = true;
objdataGridView.DataSource = m_ds1.Tables[0];
MessageBox.Show("Updated successfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
dr1.Close();
}
private void viewToolStripMenuItem_Click(object sender, EventArgs e)
{
objdiaplaypanel.Visible = true;
objtoolStrip.Visible = true;
btnview.Visible = true;
cmbEmpIDtoolStripComboBox.Focus();

grpAllocate.Visible = false;
cmbEmpIDtoolStripComboBox.Items.Add("Select");
cmbProjIDtoolStripComboBox.Items.Add("Select");
cmbEmpIDtoolStripComboBox.Items.Add("All");
cmbProjIDtoolStripComboBox.Items.Add("All");
m_con = new SqlConnection(m_strcon);
m_con.ConnectionString = connection;
m_con.Open();
SqlCommand com1;
com1 = new SqlCommand("select distinct empID from emp", m_con);
SqlDataReader dr1 = com1.ExecuteReader();
while (dr1.Read())
{
cmbEmpIDtoolStripComboBox.Items.Add(dr1.GetInt32(0));
}
dr1.Close();
SqlCommand com2 = new SqlCommand("select distinct ProjID from emp", m_con);
SqlDataReader dr2 = com2.ExecuteReader();
while (dr2.Read())
{
cmbProjIDtoolStripComboBox.Items.Add(dr2.GetInt32(0));
}
dr2.Close();
m_con.Close();
m_con = new SqlConnection(m_strcon);
string strQuery = "Select * from emp";
m_da = new SqlDataAdapter(strQuery, m_con);
m_da.Fill(m_ds, "emp");
m_bs1.DataSource = m_ds.Tables[0];
objdataGridView.AutoGenerateColumns = true;
objdataGridView.DataSource = m_bs1;
objdataGridView.Visible = false;
cmbEmpIDtoolStripComboBox.SelectedIndex = 0;
cmbProjIDtoolStripComboBox.SelectedIndex = 0;
}
}
}

110,536

社区成员

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

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

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