有关C#的简单问题,SQL提取了Int格式 [英] Simple question about C#, SQL taking a Int format out

查看:108
本文介绍了有关C#的简单问题,SQL提取了Int格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 SqlCommand cmd =  SqlCommand(" 选择* FROM [t_memsch],其中[m_memcode] ='" + m_memcode_input.Text +  ,conn);
                SqlDataAdapter sda1 =  SqlDataAdapter(cmd);
                DataTable ds1 =  DataTable();

                sda1.Fill(ds1);

DateTime date1 = Convert.ToDateTime(ds1.Rows [temp] [" ].ToString() );
                         int 会话= ds1.Rows [temp] [" ].toInt16(); 



知道为什么我不能删除"m_schsession"吗?发生错误.

现在,它确实允许我使用toInt16().

解决方案

  int  session =  Int32  .Parse(ds1.Rows [temp] [" ].toString()); 





确保在m_schsession列中没有来自数据库的空值,并且我假设temp是代表行号的整数值. SQL语句.使用 SqlParameter [,会发生什么情况

SqlCommand cmd = new SqlCommand("SELECT * FROM [t_memsch] where [m_memcode]='" + m_memcode_input.Text + "'", conn);
                SqlDataAdapter sda1 = new SqlDataAdapter(cmd);
                DataTable ds1 = new DataTable();

                sda1.Fill(ds1);     

DateTime date1 = Convert.ToDateTime(ds1.Rows[temp]["m_schdate"].ToString());
                        int session = ds1.Rows[temp]["m_schsession"].toInt16();



Any idea why i cant take the "m_schsession" out? A error is occurred.

It does now allow me to use toInt16(). Only toString() is allowed.

int session =Int32.Parse(ds1.Rows[temp]["m_schsession"].toString());





Make sure you don''t have null values coming from database in m_schsession column and I assume that temp is an integer value representing row number.


Also note that you should never use literals in your SQL statements. Use SqlParameter[^] instead. That would help you with conversion related problem, protect against SQL injections etc.

For example consider what''s happening if you enter "Some''data" to m_memcode_input.Text


这篇关于有关C#的简单问题,SQL提取了Int格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆