| |

VerySource

 Forgot password?
 Register
Search
View: 672|Reply: 4

How to cut the length of a field (requires that the cut length be determined by passing parameters)

[Copy link]

3

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-1-16 12:20:01
| Show all posts |Read mode
How to extract the specific value of a field in a database table by passing parameters in one method.

My SQL statement is always wrong, I ask you experts, can you give me a code example, or give me a different solution. Attached code examples are best! !!

Thank you, I am a novice rush. A program can't do it for a long time.
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-20 22:09:01
| Show all posts
string sLenght = Request.QueryString ["Length"];
if (sLenght == null) return;

if (! this.IsInt (sLenght)) return;
int iLength = Convert.ToInt32 (sLenght);
string sValue = "DSFSFDSFSDFFFD"; // taken from the database
if (sValue.Length> iLength)
{
sValue = sValue.Substring (0, iLength);
}

Response.Write (sValue);
Ranch



bool IsInt (object o)
{
try
{
Convert.ToInt32 (o);
return true;
}
catch
{
return false;
}

}
Reply

Use magic Report

0

Threads

41

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-21 14:27:01
| Show all posts
public class classDB
 {
    public classDB ()
    {
    }
   public static SqlConnection createConnection ()
    {
     System.Data.SqlClient.SqlConnection conn = new SqlConnection ("server = .; database = pubs; uid = sa; pwd =;");
     return conn;
    }
 }


public string GetValue (string sPara)
{
SqlConnection cn = new SqlConnection ();
 string strSQL = "SELECT * FROM table where primary key = '" + sPara + "'";

DataSet ds = new DataSet ();

SqlCommand cmd = new SqlCommand (strSQL, cn);
cn.Open ();
    SqlDataAdapter da = new SqlDataAdapter ();

    da.SelectCommand = cmd;


sqlDataAdapter1.Fill (ds);

sqlConnection.Close ();

// Close the data connection
if (ds.Tables [0] .Rows.Count> 0)
{
return ds.Tables [0] .Rows [0] [0] .ToString ();
}
else
{
return "";
}
}
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 17:09:02
| Show all posts
Don't know if these are useful?
protected string CutString (string str, int length)
{
string newString = "";
if (str! = "")
{
if (str.Length> length)
{
newString = str.Substring (0, length) + "...";
}
else
{
newString = str;
}
}
return newString;
}

Calling method:
string str = "Truncate the specified length of the string as needed";
str = CutString (str, 10);
If you are in asp.net and want to use it in data list controls (such as: DataGrid, DataList, Repeater, etc.) to achieve the effect similar to the news list described above, you can write:
<% # CutString (DataBinder.Eval (Container.DataItem, "NewTitle"). ToString (), 16)%>
Reply

Use magic Report

3

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

 Author| Post time: 2020-2-10 18:30:02
| Show all posts
Thank you very much for giving you these multiple codes, I have read them, thank you!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list